1
+ package edu .kit .datamanager .pit .web ;
2
+
3
+ import java .util .Map ;
4
+
5
+ import edu .kit .datamanager .pit .common .RecordValidationException ;
6
+ import edu .kit .datamanager .pit .domain .PIDRecord ;
7
+ import jakarta .servlet .http .HttpServletRequest ;
8
+ import org .junit .jupiter .api .Test ;
9
+ import org .springframework .beans .factory .annotation .Autowired ;
10
+ import org .springframework .boot .test .autoconfigure .web .servlet .AutoConfigureMockMvc ;
11
+ import org .springframework .boot .test .context .SpringBootTest ;
12
+ import org .springframework .boot .test .web .client .TestRestTemplate ;
13
+ import org .springframework .boot .web .error .ErrorAttributeOptions ;
14
+ import org .springframework .boot .web .servlet .error .ErrorAttributes ;
15
+ import org .springframework .http .HttpStatus ;
16
+ import org .springframework .http .ResponseEntity ;
17
+ import org .springframework .mock .web .MockHttpServletRequest ;
18
+ import org .springframework .test .context .ActiveProfiles ;
19
+ import org .springframework .web .context .WebApplicationContext ;
20
+ import org .springframework .web .context .request .WebRequest ;
21
+ import org .springframework .web .context .request .ServletWebRequest ;
22
+
23
+ import static org .junit .jupiter .api .Assertions .*;
24
+
25
+ @ SpringBootTest (webEnvironment = SpringBootTest .WebEnvironment .RANDOM_PORT )
26
+ @ AutoConfigureMockMvc
27
+ @ ActiveProfiles ("test" )
28
+ public class ExtendedErrorAttributesTest {
29
+
30
+ @ Autowired
31
+ private TestRestTemplate restTemplate ;
32
+
33
+ @ Autowired
34
+ private WebApplicationContext webApplicationContext ;
35
+
36
+ @ Autowired
37
+ private ExtendedErrorAttributes errorAttributes ;
38
+
39
+ @ Test
40
+ public void testExtendedErrorAttributes () {
41
+ // Create a mock request to pass to the error attributes
42
+ HttpServletRequest request = new MockHttpServletRequest ();
43
+ WebRequest webRequest = new ServletWebRequest (request );
44
+
45
+ // Simulate an exception to be handled by the error attributes
46
+ request .setAttribute ("jakarta.servlet.error.exception" , new RecordValidationException (
47
+ new PIDRecord ().withPID ("asdfg" ),
48
+ "Validation failed"
49
+ ));
50
+
51
+ // Get the error attributes
52
+ Map <String , Object > attributes = errorAttributes .getErrorAttributes (webRequest , ErrorAttributeOptions .defaults ());
53
+
54
+ // Check if the custom attribute is present
55
+ assertTrue (attributes .containsKey ("pid-record" ));
56
+ }
57
+
58
+ @ Test
59
+ public void testExtendedErrorAttributesBeanRegistration () {
60
+ // Check if the ExtendedErrorAttributes bean is registered
61
+ ExtendedErrorAttributes extendedErrorAttributes = webApplicationContext .getBean (ExtendedErrorAttributes .class );
62
+ assertNotNull (extendedErrorAttributes , "ExtendedErrorAttributes bean should be registered" );
63
+ }
64
+ }
0 commit comments