Home > Software engineering >  Cucumber Junit5 ignoring @Before Annotation
Cucumber Junit5 ignoring @Before Annotation

Time:11-03

I have been working with cucumber/Java and JUnit4 (CucumberOptions) for years without trouble running the tests in both IntelliJ and maven command line.


Recently, i have been trying to make the move to JUnit5 and i was able to have all tests running in IntelliJ (only, unfortunately)

My POC project has the following structure:

junit5
-Features (folder with feature files)
-resources (folder with files used in tests)
-src
--test
---java
----stepdefs
-----SetupEnvHook
-----StepDefs
----AllTest (testrunner wip)
----JU4Test (testrunner JUnit4)
----JU5Test (testrunner Junit5)
---resources (test resources)
-junit5.iml
-pom.xml

The JU5Test.java file :

import org.junit.platform.suite.api.ConfigurationParameter;
import org.junit.platform.suite.api.SelectDirectories;
import org.junit.platform.suite.api.Suite;
import stepdefs.SetupEnvHook;
import io.cucumber.java.Before;

import static io.cucumber.core.options.Constants.*;

@Suite
@SelectDirectories("Features")
//@ConfigurationParameter(key = PARALLEL_EXECUTION_ENABLED_PROPERTY_NAME, value = "true")
@ConfigurationParameter(key = PLUGIN_PUBLISH_ENABLED_PROPERTY_NAME, value = "false")
@ConfigurationParameter(key = PLUGIN_PUBLISH_QUIET_PROPERTY_NAME, value = "true")
@ConfigurationParameter(key = PLUGIN_PROPERTY_NAME, value = "json:target/cucumber-reports/cucumber.json")
@ConfigurationParameter(key = GLUE_PROPERTY_NAME, value = "stepdefs, my.external.steps.stepdefinition")
public class JU5Test {

    @Before
    public static void beforeSuite() {
        SetupEnvHook.setEnvironment("QA");
    }
}

The beforeSuite() method is also used in the JU4Test.

When i set a breakpoint in SetupEnvHook.setEnvironment("QA"); it is completely ignored due to the fact that the Before Annotation is not working, while another breakpoint inside the same

@io.cucumber.java.BeforeAll(order = 9999)

Annotation in SetupEnvHook class is triggered correctly.


My pom file is as follows :

 <dependencies>
        <dependency>
            <groupId>my.external</groupId>
            <artifactId>steps</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.9.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
            </plugin>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>8</source>
                    <target>8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

Please ignore the my external dependancy. This dependancy is related to the stepdefinitions in the test runner file glue property.

I know that group and version values are also missing but these are all fed from the same dependancy in red so as to have more control on the versions everyone uses. This is all done in Java 8 using

org.apache.maven.plugins:maven-compiler-plugin:3.10.1
org.apache.maven.plugins:maven-surefire-plugin:3.0.0-M7
io.cucumber:cucumber-java:7.8.1
io.cucumber:cucumber-junit:7.8.1
io.cucumber:cucumber-junit-platform-engine:7.8.1
org.junit.jupiter:junit-jupiter-api:5.9.1
org.junit.jupiter:junit-jupiter-engine:5.9.1
org.junit.platform:junit-platform-suite-api:1.9.1
org.junit.platform:junit-platform-suite-engine:1.9.1

I already tried using different Annotations not only from io.cucumber.java but also from org.junit (which is basically JUnit4) and org.junit.jupiter.api with no success obviously.

Running through maven command line ends up with :

Results :

Tests run: 0, Failures: 0, Errors: 0, Skipped: 0

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12.4:test (default-test) on project junit5: No tests were executed! 

It does not however state that 'no tests were found', had that issue initially and got it solved.

From looking at the error i suspect i may have something missing from the pom.xml surefire plugin but i cannot figure out what. (this pom is the same used to run the JU4Test without issues)

Anyone else have any thoughts on what i can try next? or better yet, the solution for this xD

Edit: remove images

CodePudding user response:

It does not however state that 'no tests were found', had that issue initially and got it solved.

From looking at the error i suspect i may have something missing from the pom.xml surefire plugin but i cannot figure out what. (this pom is the same used to run the JU4Test without issues)

From your description it is impossible to say what is wrong with your project. Your list of depencies includes dependencies not included in your POM.

You may want to consider starting your project from scratch. You can use the https://github.com/cucumber/cucumber-java-skeleton for that.

When i set a breakpoint in SetupEnvHook.setEnvironment("QA"); it is completely ignored due to the fact that the Before Annotation is not working

The reason the @Before annotation is ignored is because you are using a Cucumber annotation on a class that is not part of the glue path.

Though I suspect you are trying to find a mapping for JUnit 4s @BeforeClass. Currently there is not such thing in JUnit 5s Suite Engine. If you need it, you should consider making a pull requests.

Alternatively you could create a package with a single class for each environment and use Cucumbers @BeforeAll hooks to set the environment. Then for each @Suite you configure a different glue path to include those hooks.

Though I think it would be even better to read the target environment from an environment variable and have it default to something sane. You can then use different CI jobs for each environment.

  • Related