【发布时间】:2015-01-09 12:47:31
【问题描述】:
我有一个小型 Spring Boot 应用程序,在“/health”处定义了健康端点。这是在代码中定义的:
@Component
public class MyHealthEndpoint implements HealthIndicator {
public Health health() {
# ...
# returns health info as JSON here
# ...
}
}
因此,当运行应用程序时,访问 localhost/health 的端点会返回一些应用程序健康信息。
我的问题是:最好的测试方法是什么?
我想对 MyHealthEndpoint 类进行单元测试,我尝试在我的测试类中使用 @Autowired 实例化它,但它不起作用。 还尝试使用
在测试类中实例化它MyHealthEndpoint testHealthEndpoint = new MyHealthEndpoint();
但这只是返回一个没有健康信息的空新类,所以显然 Spring Boot 中的依赖注入/IOC 没有注册它,或者我只是 Spring 的新手,不知道该怎么做正确。
是通过启动应用程序并直接对端点运行测试 http GET 调用然后断言返回的 JSON 来运行集成测试的唯一解决方案,还是您有更好的想法?
【问题讨论】:
标签: java spring unit-testing