22
33import io .micrometer .tracing .Span ;
44import io .micrometer .tracing .Tracer ;
5+ import io .micrometer .tracing .TraceContext ;
56import org .junit .jupiter .api .Test ;
67import org .springframework .http .HttpStatus ;
78import org .springframework .http .ResponseEntity ;
89import org .springframework .web .server .ResponseStatusException ;
9-
1010import uk .gov .hmcts .cp .openapi .model .ErrorResponse ;
11- import io .micrometer .tracing .TraceContext ;
1211
13- import static org .junit .jupiter .api .Assertions .*;
14- import static org .mockito .Mockito .*;
12+ import java .time .Instant ;
13+
14+ import static org .junit .jupiter .api .Assertions .assertEquals ;
15+ import static org .junit .jupiter .api .Assertions .assertNotNull ;
16+ import static org .junit .jupiter .api .Assertions .assertTrue ;
17+ import static org .mockito .Mockito .mock ;
18+ import static org .mockito .Mockito .when ;
1519
1620class GlobalExceptionHandlerTest {
1721
1822 @ Test
19- void handleResponseStatusExceptionShouldReturnErrorResponseWithCorrectFields () {
23+ void handle_response_status_exception_should_return_error_response_with_correct_fields () {
2024 // Arrange
2125 final Tracer tracer = mock (Tracer .class );
2226 final Span span = mock (Span .class );
@@ -29,18 +33,32 @@ void handleResponseStatusExceptionShouldReturnErrorResponseWithCorrectFields() {
2933 final GlobalExceptionHandler handler = new GlobalExceptionHandler (tracer );
3034
3135 final String reason = "Test error" ;
32- final ResponseStatusException exception = new ResponseStatusException (HttpStatus .NOT_FOUND , reason );
36+ final ResponseStatusException exception =
37+ new ResponseStatusException (HttpStatus .NOT_FOUND , reason );
38+
39+ final Instant beforeCall = Instant .now ();
3340
3441 // Act
35- final ResponseEntity <ErrorResponse > response = handler .handleResponseStatusException (exception );
42+ final ResponseEntity <ErrorResponse > response =
43+ handler .handleResponseStatusException (exception );
44+
45+ final Instant afterCall = Instant .now ();
3646
3747 // Assert
3848 assertEquals (HttpStatus .NOT_FOUND , response .getStatusCode ());
49+
3950 final ErrorResponse error = response .getBody ();
4051 assertNotNull (error );
52+
4153 assertEquals ("404" , error .getError ());
4254 assertEquals (reason , error .getMessage ());
43- assertNotNull (error .getTimestamp ());
4455 assertEquals ("test-trace-id" , error .getTraceId ());
56+
57+ assertNotNull (error .getTimestamp ());
58+ assertTrue (
59+ !error .getTimestamp ().isBefore (beforeCall )
60+ && !error .getTimestamp ().isAfter (afterCall ),
61+ "Timestamp should be between beforeCall and afterCall"
62+ );
4563 }
46- }
64+ }
0 commit comments