Java – run the cucumber test using JUnit categories through Maven

I have a maven project with multiple modules and a common parent module In this project, some unit tests run with JUnit and surefire, and BDD cucumber integration tests I want to run two separate jobs, one for running all unit tests and the other for running BDD / integration tests To do this, I annotated my BDD runner class with JUnit class annotation, as follows:

@RunWith(Cucumber.class)
@CucumberOptions(
                tags = { "@ATagToBeRun","~@ATagNotToBeRun","~@ToBeImplemented" },dryRun = false,strict = true,features = "src/test/resources/cucumber/testing",glue = { "com.some.company.test","com.some.company.another.test"})
@Category(value = IntegrationTest.class)
public class FlowExecutionPojoTest {
}

I created a maven configuration file in the parent POM, which uses the Maven surefire plugin function to filter tests according to groups This is my Maven configuration:

<dependencies>
        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-java</artifactId>
            <version>1.2.4</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-junit</artifactId>
            <version>1.2.4</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-spring</artifactId>
            <version>1.2.4</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-jvm-deps</artifactId>
            <version>1.0.5</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <profile>
        <id>testJewels</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <modules>
            <module>../my-module</module>
        </modules>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.19.1</version>
                    <configuration>
                        <groups>com.some.company.IntegrationTest</groups>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>

What I expect is that when I run MVN test - ptestjewels, I use the class annotation contained in < groups > Labels should be implemented In fact, no annotated classes are executed

An interesting note is that this works when I use Maven surefire plugin version 2.18, but from version 2.18 At first, it didn't According to the document at the bottom of the page, version 2.18 There are changes to inheritance categories in 1, but in my case they are

Solution

I found that there is actually a pull request in the cucumber JVM repository to fix this particular problem The problem is that when JUnit filters test runner classes according to the @ category annotation, it also checks all subclasses Running In the case of cucumber test of feature file, the representation is also checked All classes of the feature file (each. Feature file in an instance of the featurerunner class) Because there are no comments on the featurerunner class, the test will be filtered out and will not run (before version 2.18.1, Maven surefire plugin did not check the comments of subclasses, so this is not a problem

The solution to the specific settings I described above (applicable to all BDD tests running in a specific module) is to override the configuration in the < groups > sub module, as follows:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
                <groups combine.self="override"></groups>
            </configuration>
        </plugin>
    </plugins>
</build>

This obviously does not apply to different settings, so unfortunately, the cucumber team has not merged the PR I mentioned above

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>