控制器测试中没有 'org.springframework.boot.actuate.health.HealthEndpoint' 类型的合格 bean

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

我写了一个结合执行器信息的控制器。

@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();
    }
}

在我的测试中出现错误 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() {
    }
}

如何在控制器测试 (@SpringBootTest/@WebMvcTest) 中激活默认 spring 执行器 bean?

我想 我正在缩小上下文范围 是您问题的答案:Spring 仅将控制器包含在其上下文中,跳过其他所有内容。也尝试包括 HealthEndpointAutoConfiguration

我通过创建 TestApplication.java 解决了这个问题,它只包含我的应用程序的一个控制器。然后我通过将测试应用程序加载到上下文 @SpringBootTest(classes = TestApplication.class).

中进行测试
@SpringBootApplication
@Import(AppStatusRestController.class)
public class TestApplication {
    public static void main(String[] args) {
        SpringApplication.run(TestApplication.class, args);
    }
}
@SpringBootTest(classes = TestApplication.class)
@AutoConfigureMockMvc
@TestPropertySource(properties = { "management.endpoints.web.exposure.include=health,info" })
class AppStatusRestControllerTest {

    @Autowired
    private MockMvc mockMvc;

    @Test
    void version() throws Exception {
        MvcResult mvcResult = this.mockMvc.perform(get("/status")).andReturn();
        String contentAsString = mvcResult.getResponse().getContentAsString();
        ObjectMapper objectMapper = new ObjectMapper();
        Status result = objectMapper.readValue(contentAsString, Status.class);
        assertEquals(result.getStatus(), "UP");
    }
}

您可以使用 @SpringBootTest() 而不使用 类,它也可以工作。在这种情况下,Spring 将加载执行测试的完整上下文。

@SpringBootTest()
@AutoConfigureMockMvc
@TestPropertySource(properties = { "management.endpoints.web.exposure.include=health,info" })
class AppStatusRestControllerTest {

    @Autowired
    private MockMvc mockMvc;

    @Test
    void version() throws Exception {
        MvcResult mvcResult = this.mockMvc.perform(get("/status")).andReturn();
        String contentAsString = mvcResult.getResponse().getContentAsString();
        ObjectMapper objectMapper = new ObjectMapper();
        Status result = objectMapper.readValue(contentAsString, Status.class);
        assertEquals(result.getStatus(), "UP");
    }
}