diff --git a/src/main/java/com/lpvs/LicensePreValidationService.java b/src/main/java/com/lpvs/LicensePreValidationService.java index 34a4bda5..2a48bb62 100644 --- a/src/main/java/com/lpvs/LicensePreValidationService.java +++ b/src/main/java/com/lpvs/LicensePreValidationService.java @@ -60,7 +60,7 @@ public static void main(String[] args) { exitHandler = applicationContext.getBean(LPVSExitHandler.class); } catch (IllegalArgumentException e) { log.error("An IllegalArgumentException occurred: " + e.getMessage()); - System.exit(1); + exitHandler.exit(-1); } catch (Exception e) { log.info("LPVS application is being closed."); } diff --git a/src/test/java/com/lpvs/LicensePreValidationServiceTest.java b/src/test/java/com/lpvs/LicensePreValidationServiceTest.java index 667f85bf..05208b1f 100644 --- a/src/test/java/com/lpvs/LicensePreValidationServiceTest.java +++ b/src/test/java/com/lpvs/LicensePreValidationServiceTest.java @@ -6,12 +6,20 @@ */ package com.lpvs; +import com.lpvs.util.LPVSExitHandler; +import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.mockito.MockedConstruction; +import org.mockito.MockedStatic; +import org.mockito.Mockito; +import org.springframework.boot.SpringApplication; +import org.springframework.context.ConfigurableApplicationContext; import org.springframework.core.task.TaskExecutor; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; +import java.lang.reflect.Field; + import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.Mockito.*; @@ -19,9 +27,19 @@ public class LicensePreValidationServiceTest { final int testNumCores = 42; LicensePreValidationService licensePreValidationService; + private MockedStatic mockedStatic; + @BeforeEach void setUp() { licensePreValidationService = new LicensePreValidationService(42); + mockedStatic = Mockito.mockStatic(SpringApplication.class); + } + + @AfterEach + public void tearDown() { + if (mockedStatic != null) { + mockedStatic.close(); + } } @Test @@ -41,4 +59,64 @@ public void testGetAsyncExecutor() { verifyNoMoreInteractions(mocked_constructed_executor); } } + + @Test + public void testMain() { + ConfigurableApplicationContext applicationContext = + Mockito.mock(ConfigurableApplicationContext.class); + LPVSExitHandler exitHandler = Mockito.mock(LPVSExitHandler.class); + String[] args = new String[0]; + + mockedStatic + .when(() -> SpringApplication.run(LicensePreValidationService.class, args)) + .thenReturn(applicationContext); + Mockito.when(applicationContext.getBean(LPVSExitHandler.class)).thenReturn(exitHandler); + LicensePreValidationService.main(args); + Mockito.verify(applicationContext).getBean(LPVSExitHandler.class); + } + + @Test + public void testMain_IllegalAccessException_N() + throws NoSuchFieldException, IllegalAccessException { + ConfigurableApplicationContext applicationContext = + Mockito.mock(ConfigurableApplicationContext.class); + LPVSExitHandler exitHandler = Mockito.mock(LPVSExitHandler.class); + String[] args = new String[0]; + + mockedStatic + .when(() -> SpringApplication.run(LicensePreValidationService.class, args)) + .thenReturn(applicationContext); + + Field exitHandlerField = LicensePreValidationService.class.getDeclaredField("exitHandler"); + exitHandlerField.setAccessible(true); + exitHandlerField.set(null, exitHandler); + + Mockito.doThrow(new IllegalArgumentException("Test IllegalArgumentException")) + .when(applicationContext) + .getBean(LPVSExitHandler.class); + LicensePreValidationService.main(args); + Mockito.verify(exitHandler, Mockito.times(1)).exit(anyInt()); + } + + @Test + public void testMain_Exception_N() throws NoSuchFieldException, IllegalAccessException { + ConfigurableApplicationContext applicationContext = + Mockito.mock(ConfigurableApplicationContext.class); + LPVSExitHandler exitHandler = Mockito.mock(LPVSExitHandler.class); + String[] args = new String[0]; + + mockedStatic + .when(() -> SpringApplication.run(LicensePreValidationService.class, args)) + .thenReturn(applicationContext); + + Field exitHandlerField = LicensePreValidationService.class.getDeclaredField("exitHandler"); + exitHandlerField.setAccessible(true); + exitHandlerField.set(null, exitHandler); + + Mockito.doThrow(new RuntimeException("Test RuntimeException")) + .when(applicationContext) + .getBean(LPVSExitHandler.class); + LicensePreValidationService.main(args); + Mockito.verify(exitHandler, Mockito.times(0)).exit(anyInt()); + } }