I am trying JUnit 5 and Cucumber at Spring Boot 2.2.6 and will need both BDD scenarios and unit tests at my application. I have created a dummy ping controller corresponding feature file which are OK.
Cucumber tests are not called when I run mvn clean test
. Only JUnit test is called. However, I can run Cucumber scenarios from Intellij GUI when click on the Run Test
button for CucumberTest.java
.
Here are my classes:
DummyApplicationTests.java:
package com.a.dummy;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
@SpringBootTest
@ActiveProfiles("test")
public class DummyApplicationTests {
@Test
public void contextLoads() {
}
}
CucumberTest.java:
package com.a.dummy.bdd;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
import org.junit.runner.RunWith;
@RunWith(Cucumber.class)
@CucumberOptions(features = "src/test/resources/features")
public class CucumberTest {
}
CucumberSpringContextConfiguration.java:
package com.a.dummy.bdd;
import com.a.dummy.DummyApplication;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ActiveProfiles("test")
@ContextConfiguration(classes = DummyApplication.class)
public abstract class CucumberSpringContextConfiguration {
}
PingTest.java:
package com.a.dummy.bdd.steps;
import com.a.dummy.bdd.CucumberSpringContextConfiguration;
import io.cucumber.java.en.And;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
public class PingTest extends CucumberSpringContextConfiguration {
@When("^the client calls /ping")
public void the_client_issues_GET_ping() {
...
}
@Then("^the client receives status code of (\\d+)$")
public void the_client_receives_status_code_of(int statusCode) {
...
}
@And("^the client receives ping response")
public void the_client_receives_ping_response_body() {
...
}
}
What I am missing?
If you just upgraded to Spring 2.4.0 and maven doesn't run you integration tests, it's because:
Cucumber is based on JUnit 4. If you’re using JUnit 5, remember to include junit-vintage-engine dependency, as well. For more information, please refer to JUnit 5 documentation.
Cucumber documentation
From Spring Boot 2.4.0 reference you can fix it as follows:
If you have tests that use JUnit 4, JUnit 5’s vintage engine can be used to run them. To use the vintage engine, add a dependency on junit-vintage-engine, as shown in the following example:
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
</exclusion>
</exclusions>
</dependency>
JUnit 5 support has not yet integrated and I had to include junit-vintage due to RunWith is a Junit 4 annotation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With