1

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

@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)中激活默认的弹簧执行器 bean?

4

3 回答 3

1

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

于 2022-03-02T11:51:39.200 回答
0

我通过创建TestApplication.java仅包含我的应用程序的一个控制器来解决问题。然后我通过将测试应用程序加载到 context 来进行测试@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");
    }
}
于 2022-03-02T14:51:39.337 回答
0

您可以不使用@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");
    }
}
于 2022-03-02T15:08:54.683 回答