|
| 1 | +package info.xiaomo.actuator; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.Test; |
| 4 | +import org.springframework.beans.factory.annotation.Autowired; |
| 5 | +import org.springframework.boot.test.context.SpringBootTest; |
| 6 | +import org.springframework.boot.webmvc.test.autoconfigure.AutoConfigureMockMvc; |
| 7 | +import org.springframework.test.web.servlet.MockMvc; |
| 8 | + |
| 9 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
| 10 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; |
| 11 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
| 12 | + |
| 13 | +@SpringBootTest(classes = ActuatorMain.class) |
| 14 | +@AutoConfigureMockMvc |
| 15 | +class ActuatorEndpointTest { |
| 16 | + |
| 17 | + @Autowired |
| 18 | + private MockMvc mockMvc; |
| 19 | + |
| 20 | + @Test |
| 21 | + void 健康检查应当返回UP并包含自定义的检查项() throws Exception { |
| 22 | + mockMvc.perform(get("/actuator/health")) |
| 23 | + .andExpect(status().isOk()) |
| 24 | + .andExpect(jsonPath("$.status").value("UP")) |
| 25 | + .andExpect(jsonPath("$.components.diskSpaceRatio").exists()) |
| 26 | + .andExpect(jsonPath("$.components.diskSpaceRatio.details.freeRatio").exists()); |
| 27 | + } |
| 28 | + |
| 29 | + @Test |
| 30 | + void info端点应当返回配置的应用信息() throws Exception { |
| 31 | + mockMvc.perform(get("/actuator/info")) |
| 32 | + .andExpect(status().isOk()) |
| 33 | + .andExpect(jsonPath("$.app.name").value("SpringBootUnity actuator 示例")); |
| 34 | + } |
| 35 | + |
| 36 | + @Test |
| 37 | + void 未在白名单中的端点不应当被暴露() throws Exception { |
| 38 | + // beans 没有列在 management.endpoints.web.exposure.include 里 |
| 39 | + mockMvc.perform(get("/actuator/beans")) |
| 40 | + .andExpect(status().isNotFound()); |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + void 调用业务接口后自定义指标应当可见且计数递增() throws Exception { |
| 45 | + mockMvc.perform(get("/greeting/{name}", "xiaomo")) |
| 46 | + .andExpect(status().isOk()) |
| 47 | + .andExpect(jsonPath("$.data").value("你好, xiaomo")); |
| 48 | + |
| 49 | + mockMvc.perform(get("/greeting/{name}", "houko")) |
| 50 | + .andExpect(status().isOk()); |
| 51 | + |
| 52 | + mockMvc.perform(get("/actuator/metrics/greeting.count")) |
| 53 | + .andExpect(status().isOk()) |
| 54 | + .andExpect(jsonPath("$.name").value("greeting.count")) |
| 55 | + .andExpect(jsonPath("$.measurements[0].value").value(2.0)); |
| 56 | + } |
| 57 | + |
| 58 | +} |
0 commit comments