Search code examples
javaspringspring-bootcucumberintegration

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.

@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

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

    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


Solution

  • Main.main calls System.exit. Try Main.run instead.

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