Skip to content

Commit

Permalink
test: Add unit tests for "main" function
Browse files Browse the repository at this point in the history
Signed-off-by: Oleg Kopysov <[email protected]>
  • Loading branch information
o-kopysov committed May 28, 2024
1 parent 8a6f674 commit dcd2fbb
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/main/java/com/lpvs/LicensePreValidationService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
}
Expand Down
78 changes: 78 additions & 0 deletions src/test/java/com/lpvs/LicensePreValidationServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,40 @@
*/
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.*;

public class LicensePreValidationServiceTest {
final int testNumCores = 42;
LicensePreValidationService licensePreValidationService;

private MockedStatic<SpringApplication> mockedStatic;

@BeforeEach
void setUp() {
licensePreValidationService = new LicensePreValidationService(42);
mockedStatic = Mockito.mockStatic(SpringApplication.class);
}

@AfterEach
public void tearDown() {
if (mockedStatic != null) {
mockedStatic.close();
}
}

@Test
Expand All @@ -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());
}
}

0 comments on commit dcd2fbb

Please sign in to comment.