Skip to content

Commit

Permalink
✅ Add a test for the main class starting the Spring application
Browse files Browse the repository at this point in the history
Signed-off-by: Marcus Fihlon <[email protected]>
  • Loading branch information
McPringle committed Nov 3, 2024
1 parent 1662240 commit c43fecd
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/test/java/swiss/fihlon/apus/ApplicationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,19 @@
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.MockedStatic;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.boot.SpringApplication;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.times;

@ExtendWith(MockitoExtension.class)
class ApplicationTest {

private final PrintStream standardOut = System.out;
Expand Down Expand Up @@ -84,4 +91,20 @@ void testLongHelpOption() throws ParseException {
assertTrue(out.contains("Hash password and exit"));
}

@Test
void testMainCallsSpring() throws ParseException {
final String[] args = new String[2];
args[0] = "foo";
args[1] = "bar";

try (MockedStatic<SpringApplication> springApplication = mockStatic(SpringApplication.class)) {
springApplication.when(() -> SpringApplication.run(Application.class, args))
.thenReturn(null);
springApplication.verify(() -> SpringApplication.run(Application.class, args),
times(0));
Application.main(args);
springApplication.verify(() -> SpringApplication.run(Application.class, args),
times(1));
}
}
}

0 comments on commit c43fecd

Please sign in to comment.