From c43fecd268417f243f3997ef05f174d3e9b33360 Mon Sep 17 00:00:00 2001 From: Marcus Fihlon Date: Sun, 3 Nov 2024 20:42:05 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=85=20Add=20a=20test=20for=20the=20main?= =?UTF-8?q?=20class=20starting=20the=20Spring=20application?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Marcus Fihlon --- .../swiss/fihlon/apus/ApplicationTest.java | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/test/java/swiss/fihlon/apus/ApplicationTest.java b/src/test/java/swiss/fihlon/apus/ApplicationTest.java index df5f352..dec2133 100644 --- a/src/test/java/swiss/fihlon/apus/ApplicationTest.java +++ b/src/test/java/swiss/fihlon/apus/ApplicationTest.java @@ -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; @@ -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 = 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)); + } + } }