Search code examples
springcucumbercucumber-junit

Cucumber test doesn't start Spring boot application


When starting my Cucumber tests (both via Selenium and integration/rest), the Spring Boot (Test) application is not started automatically.

How can I configure the Cucumber start the Spring Boot application (as well)?

I tried many ways. My files are:

Cucumber main starter:

@RunWith(Cucumber.class)
@CucumberOptions(features = "src/test/resources/cucumber_integration_tests")
public class Cucumber_Integration_Test {
}

The glue code file is something like:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = Application.class, loader = SpringApplicationContextLoader.class)
@WebAppConfiguration
@IntegrationTest
public class StepDefsIntegrationTest extends SpringIntegrationTest {
    @When("^the client calls /version$")
    public void the_client_issues_GET_version() throws Throwable {
        executeGet("http://localhost:8080/version");
    }
    etc. 
}

Alternative way of starting the application in the glue code file:

@SpringBootTest( properties = "server.port=8080", classes = Application.class, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
public class wine_Cucumber_Selenium_Steps {
    private WebDriver driver;
    private String baseUrl = "http://localhost";

    private int port = 8080;

    @Given("^I visit the wine cellar home page$")
    public void goToWineCellarHomePage() {
        // Firefox
        // System.setProperty("webdriver.gecko.driver", "K:\\k_schijf\\download_via_firefox\\geckodriver-v0.11.1-win64\\geckodriver.exe");
        // driver = new FirefoxDriver();

        // Chrome
        System.setProperty("webdriver.chrome.driver", "K:\\k_schijf\\download_via_firefox\\chromedriver_win32\\chromedriver.exe");
        driver = new ChromeDriver();
        baseUrl += ":" + port;
        driver.navigate().to(baseUrl + "/index.html");
    }

Diagnose:

  • I don't see Spring started with a message
  • I get the error message "ResourceAccessException: I/O error on GET request for "http://localhost:8080/version": Connection refused"

Solution

  • You might want to use @ContextConfiguration with @SpringBootTest annotation. I have cucumber tests working example here