0

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"
tm1701
  • 7,307
  • 17
  • 79
  • 168

2 Answers2

4

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

ravinikam
  • 3,666
  • 6
  • 28
  • 25
  • Thank you for sharing your code. I tried to put the ContextConfiguration/SpringBoot at the Cucumber runtest, but it doesn't work yet. Do you have a working Cucumber, Selenium, Spring Boot example? – tm1701 Jul 16 '17 at 11:28
  • Your solution works when I add the \@ContextConfiguration with \@SpringBootTest annotations besides the Cucumber Defined Steps!! It even works when doing the Cucumber, Selenium, Spring Boot tests!! When put (only) besides the runtest, it doesn't work. – tm1701 Jul 16 '17 at 11:41
2

Thank you @ravinikam for showing a good direction. I will experiment further with the combination of ContextPath & SpringBootTest.

An answer (maybe not the best) is that I used the 1.2.4 version of Cucumber and added this code to the runTest of Cucumber. That worked.

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@ContextConfiguration(classes = Application.class, loader = SpringApplicationContextLoader.class)
@WebIntegrationTest
@RunWith(SpringJUnit4ClassRunner.class)
public @interface CucumberStepsDefinition {
}

Another idea was given by Bealdung: see this short example of an integration test.

tm1701
  • 7,307
  • 17
  • 79
  • 168