Skip to content

Commit

Permalink
test: Add unit tests for "main" function (#522)
Browse files Browse the repository at this point in the history
* test: Add unit tests for "main" function

Signed-off-by: Oleg Kopysov <[email protected]>

* fix: Modify exception handlig process

Signed-off-by: Oleg Kopysov <[email protected]>

---------

Signed-off-by: Oleg Kopysov <[email protected]>
  • Loading branch information
o-kopysov authored Jun 3, 2024
1 parent bbb8dfb commit 2668339
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/main/java/com/lpvs/LicensePreValidationService.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,14 @@ public static void main(String[] args) {
exitHandler = applicationContext.getBean(LPVSExitHandler.class);
} catch (IllegalArgumentException e) {
log.error("An IllegalArgumentException occurred: " + e.getMessage());
System.exit(1);
try {
exitHandler.exit(-1);
} catch (Exception exitException) {
log.error(
"An exception occurred during exit handling: "
+ exitException.getMessage());
System.exit(-1);
}
} catch (Exception e) {
log.info("LPVS application is being closed.");
}
Expand Down
96 changes: 96 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,82 @@ 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_IllegalAccessException_ExitHandlerIsNull_N() {
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.doThrow(new IllegalArgumentException("Test IllegalArgumentException"))
.when(applicationContext)
.getBean(LPVSExitHandler.class);
LicensePreValidationService.main(args);
Mockito.verify(exitHandler, Mockito.times(0)).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 2668339

Please sign in to comment.