0
votes

I'm using since a while Cucumber with JUnit 4 but currently I need to use it for the first time with JUnit 5 and it doesn't seem to work. I have the following dependencies:

    ...
    <dependency>
      <groupId>org.junit.platform</groupId>
      <artifactId>junit-platform-launcher</artifactId>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.junit.jupiter</groupId>
      <artifactId>junit-jupiter-engine</artifactId>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.junit.vintage</groupId>
      <artifactId>junit-vintage-engine</artifactId>
      <scope>test</scope>
    </dependency>
    ...
    <dependency>
      <groupId>io.cucumber</groupId>
      <artifactId>cucumber-java</artifactId>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>io.cucumber</groupId>
      <artifactId>cucumber-junit</artifactId>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>io.cucumber</groupId>
      <artifactId>cucumber-junit-platform-engine</artifactId>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>io.cucumber</groupId>
      <artifactId>cucumber-picocontainer</artifactId>
      <scope>test</scope>
    </dependency>
    ...

I'm using the failsafe maven plugin configured as follows:

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-failsafe-plugin</artifactId>
        <configuration>
          <groups>profileServer</groups>
          <systemPropertyVariables>
            <jacoco-agent.destfile>${project.build.directory}/jacoco-it.exec</jacoco-agent.destfile>
          </systemPropertyVariables>
        </configuration>
        <executions>
          <execution>
            <id>integration-test</id>
            <phase>integration-test</phase>
            <goals>
              <goal>integration-test</goal>
              <goal>verify</goal>
            </goals>
          </execution>
        </executions>
      </plugin>

The test class looks as follows:

@Cucumber
@Tag("profileServer")
public class CustomersCucumberIT
{
}

Here I'm using the @Tag annotation which is replacing JUnit4 @Category in order to selectively execute tests based on the element configured in the failsafe maven plugin, here above.

The fetaures file is in src/main/test/resources/features/it and its name is customers.features.

Last but not least, here is the steps class:

@Tag("profileServer")
public class CustomersCucumberSteps
{
  ...
}

Running the verify goal display the following output: ┌─────────────────────────────────────────────────────────────────────────────┐ │ Share your Cucumber Report with your team at https://reports.cucumber.io │ │ Activate publishing with one of the following: │ │ │ │ src/test/resources/cucumber.properties: cucumber.publish.enabled=true │ │ Environment variable: CUCUMBER_PUBLISH_ENABLED=true │ │ JUnit: @CucumberOptions(publish = true) │ │ │ │ More information at https://reports.cucumber.io/docs/cucumber-jvm │ │ │ │ To disable this message, add cucumber.publish.quiet=true to │ │ src/test/resources/cucumber.properties │ └─────────────────────────────────────────────────────────────────────────────┘

but simply skips the test execution as if there weren't any. Previously, with JUnit 4 I was using @CucumberOptions in order to set the features location. But with JUnit 5 this annotation isn't supported any more and I don't find any other. It seems that it is supposed to be discovered.

I've seen a couple of posts mentioning that the features files might be configured in the failsafe or surefire maven plugin with:

...
<options>
  <configurationParameters>
    ...
  </configueationParameters>
</options>
...

but this syntax doesn't seem to be supported and, anyway, I didn't find any parameter that I could use in order to configure where the steps are.

Could someone please enlighten me on that and provide also a simple example ?

Many thanks in advance.

Seymour

2

2 Answers

2
votes

https://github.com/cucumber/cucumber-jvm/blob/main/junit-platform-engine/src/main/java/io/cucumber/junit/platform/engine/Cucumber.java

https://github.com/cucumber/cucumber-jvm/tree/main/junit-platform-engine

Maven Surefire and Gradle do not yet support discovery of non-class based tests (see: gradle/#4773, SUREFIRE-1724).

As a workaround you can use the @Cucumber annotation. Cucumber will scan the package of a class annotated with @Cucumber for feature files.

So if the runner class is src/test/java/com/example/RunCucumberIT then the feature files should be in src/test/resources/com/example.

0
votes

I'm answering my own question. It appears that, as a matter of fact, Cucumber 6.7.0, that is used in my case, is unable to correctly interpret the JUnit 5 "@Tag" annotation. In the current test case presented above, commenting out the "groups" statement in the failsafe plugin configuration, which is used in order to filter the test execution, behaves as expected, i.e. all the tests are executed. Keeping this statement uncommented and whatever the "@Tag" annotation defines, all the tests are skipped.

However, JUnit 4 "@Category" annotation works as expected, i.e. the filtering is okay. Accordingly, in order to filter Cucumber tests execution based on, for example, maven profiles, only JUnit 4 style works.