Question
How can I fix the JUnit 5 'Cannot create Launcher without at least one TestEngine' error?
Answer
Encountering the error 'Cannot create Launcher without at least one TestEngine' indicates that your JUnit 5 setup is incomplete. This typically occurs when there is no TestEngine available on the classpath. This guide provides steps to resolve the issue.
<dependencies>\n <dependency>\n <groupId>org.junit.jupiter</groupId>\n <artifactId>junit-jupiter-api</artifactId>\n <version>5.8.1</version>\n <scope>test</scope>\n </dependency>\n <dependency>\n <groupId>org.junit.jupiter</groupId>\n <artifactId>junit-jupiter-engine</artifactId>\n <version>5.8.1</version>\n <scope>test</scope>\n </dependency>\n</dependencies>\n<build>\n <plugins>\n <plugin>\n <groupId>org.apache.maven.plugins</groupId>\n <artifactId>maven-surefire-plugin</artifactId>\n <version>2.22.0</version>\n </plugin>\n </plugins>\n</build>
Causes
- Not including a JUnit 5 TestEngine (like JUnit Jupiter) dependency in your Maven configuration.
- Using a version of the Maven Surefire Plugin that does not support JUnit 5.
- Missing the required dependencies in the pom.xml for running JUnit tests.
Solutions
- Add the JUnit Jupiter Engine dependency to your pom.xml file to include the required TestEngine.
- Ensure you're using a compatible version of the Maven Surefire Plugin (at least version 2.22.0).
- Double-check your pom.xml dependencies to include both junit-jupiter-api and junit-jupiter-engine.
Common Mistakes
Mistake: Did not include the junit-jupiter-engine dependency.
Solution: Add the dependency with an appropriate version to your pom.xml.
Mistake: Using an outdated version of the Maven Surefire Plugin.
Solution: Upgrade to at least version 2.22.0 to ensure JUnit 5 compatibility.
Mistake: Configuring the Test Class without an @Test annotation.
Solution: Ensure that your test methods are correctly annotated with @Test.
Helpers
- JUnit 5
- Cannot create Launcher without TestEngine
- JUnit Platform
- Maven Surefire Plugin error
- JUnit testing tutorial