Skip to content

Commit 288ec5e

Browse files
BAEL-8954 - add test
1 parent 4a7ec0a commit 288ec5e

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

logging-modules/log-all-requests/pom.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,17 @@
3232
<artifactId>spring-boot-starter-test</artifactId>
3333
<scope>test</scope>
3434
</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>
3546
</dependencies>
3647

3748
<build>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
}

0 commit comments

Comments
 (0)