Skip to content

Commit

Permalink
fix: Modify exception handlig process
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 dcd2fbb commit fbf8ff2
Show file tree
Hide file tree
Showing 2 changed files with 26 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());
exitHandler.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
18 changes: 18 additions & 0 deletions src/test/java/com/lpvs/LicensePreValidationServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,24 @@ public void testMain_IllegalAccessException_N()
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 =
Expand Down

0 comments on commit fbf8ff2

Please sign in to comment.