我写了一个控制器,它结合了执行器信息。
@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?