Spring 执行 Cucumber 测试时启动 rest-service 关闭
Spring Boot rest-service shutdown when execute Cucumber test
我使用 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();
}
}
Thie 设置有效,所以我可以对那个端点执行 POST 并执行黄瓜测试,但是
服务关闭
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())
我使用 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();
}
}
Thie 设置有效,所以我可以对那个端点执行 POST 并执行黄瓜测试,但是 服务关闭
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())