|
16 | 16 |
|
17 | 17 | package sample.actuator.log4j2;
|
18 | 18 |
|
19 |
| -import java.util.Map; |
20 |
| - |
| 19 | +import org.apache.logging.log4j.LogManager; |
| 20 | +import org.apache.logging.log4j.Logger; |
| 21 | +import org.junit.Rule; |
21 | 22 | import org.junit.Test;
|
22 | 23 | import org.junit.runner.RunWith;
|
23 | 24 |
|
24 | 25 | import org.springframework.beans.factory.annotation.Autowired;
|
| 26 | +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; |
25 | 27 | import org.springframework.boot.test.context.SpringBootTest;
|
26 |
| -import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; |
27 |
| -import org.springframework.boot.test.web.client.TestRestTemplate; |
28 |
| -import org.springframework.http.HttpStatus; |
29 |
| -import org.springframework.http.ResponseEntity; |
30 |
| -import org.springframework.test.annotation.DirtiesContext; |
| 28 | +import org.springframework.boot.test.rule.OutputCapture; |
31 | 29 | import org.springframework.test.context.junit4.SpringRunner;
|
| 30 | +import org.springframework.test.web.servlet.MockMvc; |
32 | 31 |
|
33 |
| -import static org.assertj.core.api.Assertions.assertThat; |
| 32 | +import static org.hamcrest.Matchers.containsString; |
| 33 | +import static org.hamcrest.Matchers.equalTo; |
| 34 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
| 35 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; |
| 36 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
34 | 37 |
|
35 | 38 | /**
|
36 |
| - * Basic integration tests for service demo application. |
| 39 | + * Tests for {@link SampleActuatorLog4J2Application}. |
37 | 40 | *
|
38 | 41 | * @author Dave Syer
|
| 42 | + * @@author Stephane Nicoll |
39 | 43 | */
|
40 | 44 | @RunWith(SpringRunner.class)
|
41 |
| -@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) |
42 |
| -@DirtiesContext |
| 45 | +@SpringBootTest |
| 46 | +@AutoConfigureMockMvc |
43 | 47 | public class SampleActuatorLog4J2ApplicationTests {
|
44 | 48 |
|
| 49 | + private static final Logger logger = LogManager.getLogger(SampleActuatorLog4J2ApplicationTests.class); |
| 50 | + |
| 51 | + @Rule |
| 52 | + public OutputCapture output = new OutputCapture(); |
| 53 | + |
45 | 54 | @Autowired
|
46 |
| - private TestRestTemplate restTemplate; |
| 55 | + private MockMvc mvc; |
| 56 | + |
| 57 | + @Test |
| 58 | + public void testLogger() { |
| 59 | + logger.info("Hello World"); |
| 60 | + this.output.expect(containsString("Hello World")); |
| 61 | + } |
47 | 62 |
|
48 | 63 | @Test
|
49 |
| - public void testHome() throws Exception { |
50 |
| - @SuppressWarnings("rawtypes") |
51 |
| - ResponseEntity<Map> entity = this.restTemplate.getForEntity("/", Map.class); |
52 |
| - assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); |
53 |
| - @SuppressWarnings("unchecked") |
54 |
| - Map<String, Object> body = entity.getBody(); |
55 |
| - assertThat(body.get("message")).isEqualTo("Hello Daniel"); |
| 64 | + public void validateLoggersEndpoint() throws Exception { |
| 65 | + this.mvc.perform(get("/loggers/org.apache.coyote.http11.Http11NioProtocol")) |
| 66 | + .andExpect(status().isOk()) |
| 67 | + .andExpect(content().string(equalTo( |
| 68 | + "{\"configuredLevel\":\"WARN\"," + "\"effectiveLevel\":\"WARN\"}"))); |
56 | 69 | }
|
57 | 70 |
|
58 | 71 | }
|
0 commit comments