Skip to content
Advertisement

No qualifying bean of type ‘org.springframework.boot.actuate.health.HealthEndpoint’ in controller test

I wrote a controller which combines actuator info.

@RestController
@Slf4j
public class AppStatusRestController {
    private final HealthEndpoint healthEndpoint;
    private final InfoEndpoint infoEndpoint;

    public AppStatusRestController(HealthEndpoint healthEndpoint, InfoEndpoint infoEndpoint) {
        this.healthEndpoint = healthEndpoint;
        this.infoEndpoint = infoEndpoint;
    }

    @GetMapping("/status")
    public Status status() {
        Map<String, Object> info = infoEndpoint.info();
        return Status.builder()
                .status(healthEndpoint.health().getStatus().getCode())
                .appName("My application")
                .version(((Map<String, Object>) info.get("build")).get("version").toString())
                .buildDateTime(((Map<String, Object>) info.get("build")).get("timestamp").toString())
                .build();
    }
}

In my test I get an error No qualifying bean of type 'org.springframework.boot.actuate.health.HealthEndpoint'.

@SpringBootTest(classes = AppStatusRestController.class)
@TestPropertySource(properties = "management.endpoints.web.exposure.include=*")
class AppStatusRestControllerTest {

    @Test
    void status() {
    }
}

How can I actiavate default spring actuator beans in controller test(@SpringBootTest/@WebMvcTest)?

Advertisement

Answer

I guess I’m narrowing down the context is the answer to your question: Spring includes only the controller into its context skipping everything else. Try to include HealthEndpointAutoConfiguration too.

Advertisement