测试弹簧启动执行器定制端点

时间:2019-12-16 09:52:24

标签: junit endpoint spring-boot-actuator mockmvc

我正在尝试在springboot中测试自定义的执行器端点。

端点为:

@Component
@RestControllerEndpoint(id = "test")
public class TestController {

  @PostMapping("/1")
  public ResponseEntity<?> testEndpoint(Request request) {
    }
}

然后我使用MockMvc,

 @SpringBootTest
 @ActiveProfiles("test")
 public class MyControllerTest {

  @Autowired
  private MyController myController;

  @BeforeEach
  public void setup() {
    this.mock =
            MockMvcBuilders.standaloneSetup(myController)
                    .build();
   }

  @Test
  public void test_1() throws Exception {
    Request request = new Request();
    request.setStatus("ok");

    mock.perform(
            post("/1")
                    .contentType(MediaType.APPLICATION_JSON)
                    .content(mapper.writeValueAsString(request))
                    .andExpect(status().isOk())
            .andReturn();
  }

}

post(“ / test / 1”) post(“ / 1”)都收到404错误。 此MockMvc测试适用于普通控制器

java.lang.AssertionError: Status expected:<200> but was:<404>
Expected :200
Actual   :404

.yml文件是

management:
  server.port: 8081
  endpoints:
    web:
      base-path: /api/v1/mycontext

谢谢。

2 个答案:

答案 0 :(得分:0)

自定义端点被添加为actuator端点下的子链接。因此,您的自定义终点网址应为/actuator/test/1。在这种情况下,它应该是/api/v1/mycontext/test/1,如您设置的management.endpoints.web.base-path

答案 1 :(得分:0)

您可能需要在测试应用程序配置中启用执行器端点