首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在控制器测试中没有'org.springframework.boot.actuate.health.HealthEndpoint‘类型的合格bean

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

Stack Overflow用户
提问于 2022-03-02 10:42:08
回答 3查看 1.6K关注 0票数 1

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

代码语言:javascript
复制
@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'

代码语言:javascript
复制
@SpringBootTest(classes = AppStatusRestController.class)
@TestPropertySource(properties = "management.endpoints.web.exposure.include=*")
class AppStatusRestControllerTest {

    @Test
    void status() {
    }
}

如何在控制器测试(@SpringBootTest/@WebMvcTest)中激活缺省弹簧执行器bean?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2022-03-02 11:51:39

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

票数 1
EN

Stack Overflow用户

发布于 2022-03-02 15:08:54

您可以在没有类的情况下使用@SpringBootTest(),而且它也会工作。在本例中,Spring将加载用于执行测试的完整上下文。

代码语言:javascript
复制
@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");
    }
}
票数 1
EN

Stack Overflow用户

发布于 2022-03-02 14:51:39

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

代码语言:javascript
复制
@SpringBootApplication
@Import(AppStatusRestController.class)
public class TestApplication {
    public static void main(String[] args) {
        SpringApplication.run(TestApplication.class, args);
    }
}
代码语言:javascript
复制
@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");
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71321300

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档