0

I'm trying to test a customized actuator endpoint in springboot.

The endpoint is:

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

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

Then I use 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();
  }

}

Both post("/test/1") and post("/1") get 404 error. This MockMvc test works for normal controllers.

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

And .yml file is

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

Thanks in advance.

Vikki
  • 1,897
  • 1
  • 17
  • 24

2 Answers2

0

Custom endpoints are added as a sub link under actuator endpoint. So the url of your custom end point should be /actuator/test/1. In this case it should be /api/v1/mycontext/test/1 as you have set management.endpoints.web.base-path.

Dhaval Shewale
  • 224
  • 1
  • 6
0

you probably need to enable the actuator endpoints in your test application config

Sepultura
  • 997
  • 1
  • 9
  • 28