11package org .code .javabuilder ;
22
3- import static org .code .protocol .LoggerNames .MAIN_LOGGER ;
43import static org .junit .jupiter .api .Assertions .assertEquals ;
54import static org .mockito .ArgumentMatchers .any ;
65import static org .mockito .Mockito .*;
76
87import java .io .IOException ;
9- import java .util .logging .Handler ;
10- import java .util .logging .LogRecord ;
11- import java .util .logging .Logger ;
128import org .code .protocol .*;
139import org .junit .jupiter .api .AfterEach ;
1410import org .junit .jupiter .api .BeforeEach ;
1511import org .junit .jupiter .api .Test ;
1612import org .mockito .ArgumentCaptor ;
13+ import org .mockito .MockedStatic ;
14+ import org .mockito .Mockito ;
1715
1816class ExceptionHandlerTest {
1917
2018 private OutputAdapter outputAdapter ;
2119 private SystemExitHelper systemExitHelper ;
22- private Handler testHandler ;
2320 private ArgumentCaptor <ClientMessage > messageCaptor ;
2421 private ExceptionHandler unitUnderTest ;
22+ private MockedStatic <LoggerUtils > loggerUtilsMockedStatic ;
2523
2624 @ BeforeEach
2725 public void setUp () {
2826 outputAdapter = mock (OutputAdapter .class );
2927 systemExitHelper = mock (SystemExitHelper .class );
30- testHandler = mock (Handler .class );
3128 messageCaptor = ArgumentCaptor .forClass (ClientMessage .class );
3229 unitUnderTest = new ExceptionHandler (outputAdapter , systemExitHelper );
3330 AWSMetricClient metricClient = mock (AWSMetricClient .class );
3431 JavabuilderContext .getInstance ().register (MetricClient .class , metricClient );
35-
36- Logger .getLogger (MAIN_LOGGER ).addHandler (testHandler );
37- Logger .getLogger (MAIN_LOGGER ).setUseParentHandlers (false );
32+ loggerUtilsMockedStatic = Mockito .mockStatic (LoggerUtils .class );
3833 }
3934
4035 @ AfterEach
4136 public void tearDown () {
42- Logger .getLogger (MAIN_LOGGER ).removeHandler (testHandler );
43- Logger .getLogger (MAIN_LOGGER ).setUseParentHandlers (true );
37+ loggerUtilsMockedStatic .close ();
4438 }
4539
4640 @ Test
@@ -49,7 +43,7 @@ public void testFatalError() {
4943 unitUnderTest .handle (error );
5044
5145 // Should log, notify user, and exit
52- verify (testHandler ). publish (any (LogRecord . class ));
46+ loggerUtilsMockedStatic . verify (() -> LoggerUtils . logSevereError (any (), any (), any () ));
5347 verify (outputAdapter ).sendMessage (any (ClientMessage .class ));
5448 verify (systemExitHelper ).exit (error .getErrorCode ());
5549 }
@@ -61,7 +55,7 @@ public void testInternalServerException() {
6155 unitUnderTest .handle (internal );
6256
6357 // Should log and notify user
64- verify (testHandler ). publish (any (LogRecord . class ));
58+ loggerUtilsMockedStatic . verify (() -> LoggerUtils . logSevereError (any (), any (), any () ));
6559 verify (outputAdapter ).sendMessage (any (ClientMessage .class ));
6660 }
6761
@@ -72,7 +66,7 @@ public void testInternalFacingException() {
7266 unitUnderTest .handle (internal );
7367
7468 // Should log only
75- verify (testHandler ). publish (any (LogRecord . class ));
69+ loggerUtilsMockedStatic . verify (() -> LoggerUtils . logTrackingExceptionAsWarning (any () ));
7670 verify (outputAdapter , never ()).sendMessage (any (ClientMessage .class ));
7771 }
7872
@@ -83,7 +77,7 @@ public void testUserInitiatedException() {
8377 unitUnderTest .handle (userInitiated );
8478
8579 // Should notify user only
86- verify (testHandler , never ()). publish (any (LogRecord . class ));
80+ loggerUtilsMockedStatic . verify (() -> LoggerUtils . logSevereError (any (), any (), any ()), never ( ));
8781 verify (outputAdapter ).sendMessage (any (ClientMessage .class ));
8882 }
8983
@@ -94,7 +88,7 @@ public void testUnknownException() {
9488 unitUnderTest .handle (unknown );
9589
9690 // Should log and notify user
97- verify (testHandler ). publish (any (LogRecord . class ));
91+ loggerUtilsMockedStatic . verify (() -> LoggerUtils . logSevereError (any () ));
9892 verify (outputAdapter ).sendMessage (any (ClientMessage .class ));
9993
10094 final ClientMessage message = messageCaptor .getValue ();
0 commit comments