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