Cannot create Launcher without at least one TestEngine; consider adding an engine implementation JAR to the classpath in Junit 5

developerbhuwan :

I got following execption when i tried to run test case in junit5:

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.19:test (default-test) on project CRUD-App: Execution default-test of goal org.apache.maven.plugins:maven-surefire-plugin:2.19:test failed: There was an error in the forked process
org.junit.platform.commons.util.PreconditionViolationException: Cannot create Launcher without at least one TestEngine; consider adding an engine implementation JAR to the classpath
   at org.junit.platform.commons.util.Preconditions.condition(Preconditions.java:161)
   at org.junit.platform.launcher.core.DefaultLauncher.<init>(DefaultLauncher.java:52)
   at org.junit.platform.launcher.core.LauncherFactory.create(LauncherFactory.java:42)
   at org.junit.platform.surefire.provider.JUnitPlatformProvider.invoke(JUnitPlatformProvider.java:59)
   at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:286)
   at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:240)
   at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:121)

pom.xml

<dependencies>
    ...
    <dependency>
        <groupId>org.junit</groupId>
        <artifactId>junit5-api</artifactId>
        <version>5.0.0-SNAPSHOT</version>
        <scope>test</scope>
    </dependency>
</dependencies>
<build>
    <plugins>        
        <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.19</version>
            <dependencies>
                <dependency>
                    <groupId>org.junit.platform</groupId>
                    <artifactId>junit-platform-surefire-provider</artifactId>
                    <version>1.0.0-M2</version>
                </dependency>
            </dependencies>
        </plugin>
    </plugins>
</build>

Test Class:

public class PersonServiceTest {

    final Database database = Database.from("jdbc:h2:mem:" + App.DB_NAME);

    final PersonService personService = new PersonService(database);

    public PersonServiceTest() {
    }

    @Test
    @DisplayName("@Person#insert()")
    public void testInsert() {
        personService.insert(new PersonBuilder()
                .setId(1).setName("Bhuwan")
                .setAddress("KTM")
                .setContactNo("984849").createPerson()
        );
    }

}

Maven Goal: mvn test

Sam Brannen :

For starters, you are mixing ALPHA snapshot artifacts (i.e., org.junit:junit5-api:5.0.0-SNAPSHOT) with M2 artifacts (i.e., org.junit.platform:junit-platform-surefire-provider:1.0.0-M2), which will never work.

The Maven section in the user guide suggests to check out the pom.xml from the junit5-maven-consumer project. If you follow that example, you will end up with something like the following.

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>1.8</java.version>
    <junit.jupiter.version>5.0.0-M2</junit.jupiter.version>
    <junit.platform.version>1.0.0-M2</junit.platform.version>
</properties>

<build>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>${java.version}</source>
                <target>${java.version}</target>
            </configuration>
        </plugin>
        <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.19</version>
            <dependencies>
                <dependency>
                    <groupId>org.junit.platform</groupId>
                    <artifactId>junit-platform-surefire-provider</artifactId>
                    <version>${junit.platform.version}</version>
                </dependency>
            </dependencies>
        </plugin>
    </plugins>
</build>

<dependencies>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <version>${junit.jupiter.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <version>${junit.jupiter.version}</version>
        <scope>test</scope>
    </dependency>
</dependencies>

To write your tests, you only need the junit-jupiter-api; however, in order to run your tests you must have a TestEngine on the classpath. For JUnit Jupiter you therefore need junit-jupiter-engine on the classpath as well.

As Nicolai Parlog pointed out, you could add junit-jupiter-engine as a dependency for the maven-surefire-plugin; however, that would not include the JupiterTestEngine in the classpath for your IDE.

If you're only running tests via Maven or with a recent beta version of IntelliJ 2016 (which has built-in support for JUnit 5), then you may not care if JupiterTestEngine is on the classpath in your IDE. But... if you're using Eclipse, NetBeans, or a non-beta version of IntelliJ, you'll definitely want the JupiterTestEngine on the classpath in the IDE as well.

Regards,

Sam (core JUnit 5 committer)

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=446393&siteId=1