2

I have a Maven / Spring Boot 2.3.3 application with JUnit 5 and Cucumber (v6.5.1) tests.

The thing is that I can run OK either Unit and Integration tests via Maven, but it does not run Cucumber.

Cucumber Runner:

package cucumber.salad.api.integration.cucumber;
import org.junit.runner.RunWith;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;

@RunWith(Cucumber.class)
@CucumberOptions(
        features = "classpath:features",
        plugin = { "pretty", "html:target/reports/cucumber", "json:target/cucumber.json", "usage:target/usage.jsonx", "junit:target/junit.xml" },
        extraGlue = "cucumber.salad.api.integration.cucumber.steps"
)
public class CucumberTest {
}

Cucumber Spring Context Config:

package cucumber.salad.api.integration.cucumber;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.test.context.ContextConfiguration;
import cucumber.salad.App;
import io.cucumber.spring.CucumberContextConfiguration;

@ContextConfiguration
@CucumberContextConfiguration
@SpringBootTest(classes = App.class, webEnvironment = WebEnvironment.RANDOM_PORT)
public class CucumberSpringContextConfig {
    
    @LocalServerPort
    protected int port;
}

Steps:

package cucumber.salad.api.integration.cucumber.steps;
import static org.assertj.core.api.Assertions.assertThat;
import org.springframework.beans.factory.annotation.Autowired;
import cucumber.salad.api.integration.cucumber.CucumberSpringContextConfig;
import cucumber.salad.api.service.DummyService;
import io.cucumber.java.en.*;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class SaladStepDefinitions extends CucumberSpringContextConfig {
    
    // steps here
}

I use Surefire and Failsafe in the Maven pom.xml: https://github.com/danieldestro/cucumber-salad/blob/master/pom.xml

Here is the project repository: https://github.com/danieldestro/cucumber-salad

Command I run:

mvn clean test integration-test

But there is not a sign from Cucumber test execution.

I am missing anything or doing something wrong?

danieldestro
  • 101
  • 9

3 Answers3

4

You're using cucumber-junit which integrates Cucumber with JUnit 4. You're also using JUnit 5. JUnit 5 can execute JUnit 4 tests through the junit-vintage-engine. However you've excluded this engine from your classpath.

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

Either include the the junit-vintage-engine or use the cucumber-junit-platform-engine instead of cucumber-junit.

M.P. Korstanje
  • 6,760
  • 3
  • 28
  • 43
  • Great. You saved the day. It works fine now. I pushed a new branch with the fix: https://github.com/danieldestro/cucumber-salad/compare/fixed – danieldestro Aug 28 '20 at 16:09
0

If you use junit 5, all you have to do is to add the two cucumber dependencies (cucumber-java and cucmber-junit by excluding junit 4 from all your dependencies).

furthermore, import junit-vintage-engine so cucumber can use it.

Explanation :

If you import junit 4, all the tests with jupiter (junit 5) will be ignored, Or vice versa. You can test it by adding two classes with each version of junit (by adding @Test of each version on each class). Only one version will run.

slim
  • 389
  • 1
  • 7
  • 22
0

I had the same problem; when I ran the mvn test command the cucumber tests were not executed. I was using:

  • spring-boot:2.5.0
  • Java 11 (OpenJDK Zulu 11.0.11)
  • Dependencies
    • cucumber-core: 6.10.4
    • cucumber-java:6.10.4
    • cucumber-junit:6.10.4
    • cucumber-spring:6:10.4

What I did was exclude the junit-jupiter-engine from the spring-boot-starter-test dependency.

       <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.jupiter</groupId>
                    <artifactId>junit-jupiter-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>