From fbf8ff25f566bd14b7a75f0bab93d59286fc2dd7 Mon Sep 17 00:00:00 2001 From: Oleg Kopysov Date: Tue, 28 May 2024 16:45:04 +0300 Subject: [PATCH] fix: Modify exception handlig process Signed-off-by: Oleg Kopysov --- .../com/lpvs/LicensePreValidationService.java | 9 ++++++++- .../lpvs/LicensePreValidationServiceTest.java | 18 ++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/lpvs/LicensePreValidationService.java b/src/main/java/com/lpvs/LicensePreValidationService.java index 2a48bb62..79b375f5 100644 --- a/src/main/java/com/lpvs/LicensePreValidationService.java +++ b/src/main/java/com/lpvs/LicensePreValidationService.java @@ -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."); } diff --git a/src/test/java/com/lpvs/LicensePreValidationServiceTest.java b/src/test/java/com/lpvs/LicensePreValidationServiceTest.java index 05208b1f..d0a489bc 100644 --- a/src/test/java/com/lpvs/LicensePreValidationServiceTest.java +++ b/src/test/java/com/lpvs/LicensePreValidationServiceTest.java @@ -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 =