简体   繁体   English

Spring 执行 Cucumber 测试时启动休息服务关闭

[英]Spring Boot rest-service shutdown when execute Cucumber test

I made a rest service using Spring Boot 2 and Cucumber IO 5.7.0 with one endpoint just to execute the cucumber test. I made a rest service using Spring Boot 2 and Cucumber IO 5.7.0 with one endpoint just to execute the cucumber test.

@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());

    }
}
}

As you can see I execute the Cucumber Main to execute the steps如您所见,我执行Cucumber Main来执行这些步骤

Feature: Checking all health
  Scenario: Localhost is up
    When When 'localhost:8080/actuator/heatlth' is called
    And Response is UP

The definition is this定义是这样的

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");

}
}

And

@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 setup works, so I can execute a POST to that endpoitn and execute the cucumber test, but the service shutdown Thie 设置有效,因此我可以对该端点执行 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

I want to prevent that but I don't know how我想防止这种情况,但我不知道如何

Main.main calls System.exit . Main.main调用System.exit Try Main.run instead.改用Main.run

io.cucumber.core.cli.Main.run(argv, Thread.currentThread().getContextClassLoader())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM