简体   繁体   中英

How to create a test runner class with JUnit5, Cucumber and run test from jar file (maven shade)?

I am trying to switch from JUni4 & io.cukes to JUnit5 & io.cucumber. Currently i run "mvn clean package" command to create a jar file. And then run the test using java command (java -jar "-Dcucumber.options...." jar file) to run the tests. The class name is also mentioned in POM file as well.

Any idea how to create a test runner class (RunCukes.java in this case) with JUni5 and latest version of cucumber?.

Currently i looking for an alternative to 'JUnitCore.main(className)' in JUnit5... no luck yet. @CucumberOptions & @RunWith are not supported either by new libraries either...

RunCukes.java

import org.junit.runner.JUnitCore;
import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
import managers.AutUtilities;

@RunWith(Cucumber.class)
@CucumberOptions(
        features = {"./features"},  
        glue={"mercurysd"},
        plugin = {"pretty","html:target/cucumber-reports", "json:target/cucumber-reports/report.json"}
)

public class RunCukes {
    
    public static void main(String[] args) {
        AutUtilities.call().setLogging();
        JUnitCore.main(RunCukes.class.getName());
    }
)

POM

             <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.2.0</version>
                <executions>
                    <!-- Attach the shade goal into the package phase -->
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <transformer
                                    implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>mercurypo.RunCukes</mainClass>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

The JUnit 5 equivalent of JUnitCore.main is the JUnit Platform Launcher API . Instead of using cucumber-junit you would use cucumber-junit-platform-engine .

public class RunCucumber {

   public static void main(String[] args) {

      LauncherDiscoveryRequest request = request()
              .selectors(
                      selectDirectory("path/to/features")
              )
              .build();

      Launcher launcher = LauncherFactory.create();
      launcher.execute(request);
    }
}

Though I don't know how either will behave in a shaded jar.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM