Bhuwan Prasad Upadhyay
Bhuwan Prasad Upadhyay

Reputation: 3056

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

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

Upvotes: 74

Views: 68002

Answers (5)

Ali
Ali

Reputation: 1590

simply adding below dependency solved this prob.

testImplementation 'org.junit.jupiter:junit-jupiter-engine:5.8.2'

Upvotes: 22

lainatnavi
lainatnavi

Reputation: 1613

Ended up here since getting the same error using Gradle and JUnit 4. The solution in this case can be found in the docs:

The JUnit Platform can run JUnit 4 based tests as long as you configure a testImplementation dependency on JUnit 4 and a testRuntimeOnly dependency on the JUnit Vintage TestEngine implementation similar to the following.

A test engine is also needed:

dependencies {
    testImplementation("junit:junit:4.13.2")
    testRuntimeOnly("org.junit.vintage:junit-vintage-engine:5.8.2")
}

Upvotes: 31

Sam Brannen
Sam Brannen

Reputation: 31177

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), won't 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)

Upvotes: 49

FabianoLothor
FabianoLothor

Reputation: 2977

I solve it changing

classpath "org.springframework.boot:spring-boot-gradle-plugin:2.1.8.RELEASE"

to

classpath "org.springframework.boot:spring-boot-gradle-plugin:2.2.0.RELEASE"

Upvotes: 0

Nicolai Parlog
Nicolai Parlog

Reputation: 51040

The Maven Surefire plugin not only needs the JUnit 5 provider but also a TestEngine implementation to run tests with. To quote the JUnit 5 docs:

In order to have Maven Surefire run any tests at all, a TestEngine implementation must be added to the runtime classpath.

In accordance with that the following works:

<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-M4</version>
                </dependency>
                <dependency>
                    <groupId>org.junit.jupiter</groupId>
                    <artifactId>junit-jupiter-engine</artifactId>
                    <version>5.0.0-M4</version>
                </dependency>
            </dependencies>
        </plugin>
    </plugins>
</build>

<dependencies>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <version>5.0.0-M4</version>
        <scope>test</scope>
    </dependency>
</dependencies>

Note that this configuration makes the engine a dependency of the surefire plugin, not of your test code.

Upvotes: 13

Related Questions