【问题标题】:Spring Boot rest-service shutdown when execute Cucumber test执行 Cucumber 测试时 Spring Boot 休息服务关闭
【发布时间】:2020-08-27 03:44:12
【问题描述】:

我使用 Spring Boot 2 和 Cucumber IO 5.7.0 制作了一项休息服务,其中一个端点只是为了执行黄瓜测试。

@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
  • @M.P.Korstanje 它需要一个 ClassLoader public static byte run(String[] argv, ClassLoader classLoader) {
  • 这不应该阻止你。看Main.main的实现。
  • 它有效,将其添加为答案。谢谢

标签: java spring spring-boot cucumber integration


【解决方案1】:

Main.main 调用 System.exit。请改用Main.run

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

【讨论】:

  • Cucumber-io 5.7.0 已弃用 Main,还有其他选择吗?
  • Java 文档对弃用有何说明?日志记录告诉你什么?
  • 内部没有 java 文档 :(
  • 你应该下载它。
猜你喜欢
  • 1970-01-01
  • 2018-01-23
  • 2016-04-02
  • 1970-01-01
  • 2015-04-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多