我使用 Spring Boot 2 和 Cucumber IO 5.7.0 创建了一个休息服务,其中一个端点只是为了执行 Cucumber 测试。
@Controller
@RequestMapping("/api/v1")
public class IntegrationTestController {
@PostMapping("/integration")
public void runCucumber(){
try {
Main.main(new String[]{"--glue",
"pnc/aop/integration", // the package which contains the glue classes
"src/main/resources/features/healthcheck.feature"});
} catch (Throwable ex) {
log.error(ex.getLocalizedMessage());
}
}
}
如您所见,我执行了 Cucumber Main
来执行这些步骤
Feature: Checking all health
Scenario: Localhost is up
When When 'localhost:8080/actuator/heatlth' is called
And Response is UP
定义是这样的
public class HealCheckSteps extends SpringIntegrationTest {
private Health response;
@When("When {string} is called")
public void whenHealthCheckIsCalled(String url) {
response = checkStatus(url);
}
@And("Response is UP")
public void andReponseIsUp() {
Assert.assertEquals(response.getStatus().getCode(), "UP");
}
}
和
@RunWith(SpringRunner.class)
@SpringBootTest(
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class SpringIntegrationTest {
protected RestTemplate restTemplate = new RestTemplate();
public SpringIntegrationTest() {
this.restTemplate = new RestTemplate();
}
public Health checkStatus(String url) {
try {
JsonNode response = restTemplate.getForObject(url, JsonNode.class);
if (response.get("status").asText().equalsIgnoreCase("UP")) {
return Health.up().build();
}
} catch (Exception ex) {
return Health.down(ex).build();
}
return Health.down().build();
}
}
这个设置有效,所以我可以向该端点执行 POST 并执行 cucumber 测试,但是 服务关闭
1 Scenarios (1 passed)
2 Steps (2 passed)
0m1.454s
2020-05-10 23:58:53.822 INFO 43182 --- [extShutdownHook] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor'
2020-05-10 23:58:53.822 INFO 43182 --- [extShutdownHook] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor'
Process finished with exit code 0
我想阻止这种情况,但我不知道如何做
最佳答案
Main.main
调用 System.exit
。尝试使用 Main.run
来代替。
io.cucumber.core.cli.Main.run(argv, Thread.currentThread().getContextClassLoader())
关于java - 执行 Cucumber 测试时 Spring Boot 休息服务关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61732440/