我正在尝试在 JUnit 中为运行状况检查服务方法编写一个测试用例,作为其中的一部分,我想在测试中实例化来自 spring 的环境对象。
在应用程序中我能够
@Autowire
Environment env;
Run Code Online (Sandbox Code Playgroud)
这给了我一个实例化的对象,但测试类的情况并非如此。
使这种情况变得复杂的是,这是一个“插入式”服务/控制器,旨在导入到其他应用程序中,而不是单独运行,因此缺乏 Spring 应用程序所需的典型类。
public class HealthCheckServiceTest {
@Autowired
Environment environment;
private HealthCheckService healthCheckService;
@Before
public void loadGitPropertiesFile() throws Exception {
healthCheckService = new HealthCheckService();
healthCheckService.setEnvironment(environment);
}
@Test
public void testStatus() throws Exception {
//Test to see if Health Check results are returned
}
}
Run Code Online (Sandbox Code Playgroud)