File tree 2 files changed +52
-0
lines changed
logging-modules/log-all-requests
src/test/java/com/baeldung/logallrequests
2 files changed +52
-0
lines changed Original file line number Diff line number Diff line change 32
32
<artifactId >spring-boot-starter-test</artifactId >
33
33
<scope >test</scope >
34
34
</dependency >
35
+ <dependency >
36
+ <groupId >com.github.stefanbirkner</groupId >
37
+ <artifactId >system-rules</artifactId >
38
+ <version >1.19.0</version >
39
+ <scope >test</scope >
40
+ </dependency >
41
+ <dependency >
42
+ <groupId >ch.qos.logback</groupId >
43
+ <artifactId >logback-classic</artifactId >
44
+ <scope >test</scope >
45
+ </dependency >
35
46
</dependencies >
36
47
37
48
<build >
Original file line number Diff line number Diff line change
1
+ package com .baeldung .logallrequests ;
2
+
3
+ import static org .assertj .core .api .Assertions .assertThat ;
4
+
5
+ import org .junit .jupiter .api .Test ;
6
+ import org .junit .jupiter .api .extension .ExtendWith ;
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 .system .CapturedOutput ;
11
+ import org .springframework .boot .test .system .OutputCaptureExtension ;
12
+ import org .springframework .test .context .junit .jupiter .SpringExtension ;
13
+ import org .springframework .test .web .servlet .MockMvc ;
14
+ import org .springframework .test .web .servlet .request .MockMvcRequestBuilders ;
15
+
16
+ @ ExtendWith ({ SpringExtension .class , OutputCaptureExtension .class })
17
+ @ SpringBootTest
18
+ @ AutoConfigureMockMvc
19
+ public class LoggingFilterTest {
20
+
21
+ @ Autowired
22
+ private MockMvc mockMvc ;
23
+
24
+ @ Test
25
+ public void testRequestIsLogged (CapturedOutput output ) throws Exception {
26
+ mockMvc .perform (MockMvcRequestBuilders .get ("/api/hello" )
27
+ .header ("X-Test-Header" , "HeaderValue" ))
28
+ .andReturn ();
29
+
30
+ assertThat (output .getAll ()).contains ("Incoming Request: [GET] /api/hello" );
31
+ }
32
+
33
+ @ Test
34
+ public void testResponseIsLogged (CapturedOutput output ) throws Exception {
35
+ mockMvc .perform (MockMvcRequestBuilders .get ("/api/hello" ))
36
+ .andReturn ();
37
+
38
+ assertThat (output .getAll ()).contains ("Outgoing Response for [GET] /api/hello: Status = 200" );
39
+ }
40
+
41
+ }
You can’t perform that action at this time.
0 commit comments