11package uk .bot_by .aws_lambda .slf4j .json_output ;
22
33import static org .hamcrest .MatcherAssert .assertThat ;
4+ import static org .hamcrest .core .IsEqual .equalTo ;
45import static org .hamcrest .core .StringStartsWith .startsWith ;
56import static org .junit .jupiter .api .Assertions .assertAll ;
67import static org .junit .jupiter .api .Assertions .assertDoesNotThrow ;
8+ import static org .junit .jupiter .api .Assertions .assertFalse ;
79import static org .junit .jupiter .api .Assertions .assertTrue ;
810import static org .mockito .ArgumentMatchers .any ;
911import static org .mockito .ArgumentMatchers .anyString ;
@@ -248,16 +250,17 @@ void showLogName() {
248250 stringCaptor .getValue (), true );
249251 }
250252
251- @ DisplayName ("Print a stack trace " )
253+ @ DisplayName ("Print a throwable " )
252254 @ Test
253- void stackTrace () {
255+ void throwableWithMessage () {
254256 // given
255257 var throwable = mock (Throwable .class );
256258
257259 doAnswer (invocationOnMock -> {
258260 invocationOnMock .getArgument (0 , PrintStream .class ).println ("*" );
259261 return null ;
260262 }).when (throwable ).printStackTrace (isA (PrintStream .class ));
263+ when (throwable .getMessage ()).thenReturn ("test message" );
261264 when (configuration .requestId ()).thenReturn ("request#" );
262265
263266 // when
@@ -269,7 +272,40 @@ void stackTrace() {
269272
270273 var jsonObject = new JSONObject (stringCaptor .getValue ());
271274
272- assertAll ("thread ID" , () -> assertTrue (jsonObject .has ("stack-trace" ), "field exists" ),
275+ assertAll ("throwable" , () -> assertTrue (jsonObject .has ("throwable-class" )),
276+ () -> assertThat ("class" , jsonObject .getString ("throwable-class" ),
277+ equalTo ("java.lang.Throwable" )), () -> assertTrue (jsonObject .has ("throwable-message" )),
278+ () -> assertThat ("message" , jsonObject .getString ("throwable-message" ),
279+ equalTo ("test message" )),
280+ () -> assertTrue (jsonObject .has ("stack-trace" ), "field exists" ),
281+ () -> assertThat ("field value" , jsonObject .getString ("stack-trace" ), startsWith ("*" )));
282+ }
283+
284+ @ DisplayName ("Print a throwable without message" )
285+ @ Test
286+ void throwableWithoutMessage () {
287+ // given
288+ var throwable = mock (Throwable .class );
289+
290+ doAnswer (invocationOnMock -> {
291+ invocationOnMock .getArgument (0 , PrintStream .class ).println ("*" );
292+ return null ;
293+ }).when (throwable ).printStackTrace (isA (PrintStream .class ));
294+ when (configuration .requestId ()).thenReturn ("request#" );
295+
296+ // when
297+ loggerOutput .log (configuration , lambdaLogger , null , Level .ERROR , "test error message" ,
298+ throwable );
299+
300+ // then
301+ verify (lambdaLogger ).log (stringCaptor .capture ());
302+
303+ var jsonObject = new JSONObject (stringCaptor .getValue ());
304+
305+ assertAll ("throwable" , () -> assertTrue (jsonObject .has ("throwable-class" )),
306+ () -> assertThat ("class" , jsonObject .getString ("throwable-class" ),
307+ equalTo ("java.lang.Throwable" )), () -> assertFalse (jsonObject .has ("throwable-message" )),
308+ () -> assertTrue (jsonObject .has ("stack-trace" ), "field exists" ),
273309 () -> assertThat ("field value" , jsonObject .getString ("stack-trace" ), startsWith ("*" )));
274310 }
275311
0 commit comments