Question
What are the differences between the Maven modules junit-jupiter-api and junit-jupiter-engine, and do I need to include both in my build.gradle?
testCompile("org.junit.jupiter:junit-jupiter-engine:${junitVersion}")
testCompile("org.junit.jupiter:junit-jupiter-api:${junitVersion}")
Answer
JUnit 5 (JUnit Jupiter) comprises several components, two of the most important being junit-jupiter-api and junit-jupiter-engine. Understanding their roles and the necessity of including them in your project can enhance your testing capabilities.
dependencies {
testCompile("org.junit.jupiter:junit-jupiter-api:${junitVersion}")
testCompile("org.junit.jupiter:junit-jupiter-engine:${junitVersion}")
}
Causes
- JUnit is modular, with different artifact responsibilities.
- JUnit Jupiter API contains annotations and interfaces for writing tests.
- JUnit Jupiter Engine is responsible for executing the tests written using the API.
Solutions
- Include both dependencies in your build.gradle file for optimal functionality:
- `testCompile("org.junit.jupiter:junit-jupiter-api:${junitVersion}")`
- `testCompile("org.junit.jupiter:junit-jupiter-engine:${junitVersion}")`
- If only the engine is included, you will not have access to the test annotations and structures needed to define tests.
Common Mistakes
Mistake: Only including junit-jupiter-engine but not junit-jupiter-api.
Solution: You need both dependencies for proper testing. Always include junit-jupiter-api if you are writing tests.
Mistake: Neglecting to include versioning for dependencies.
Solution: Always specify the version of the dependencies you're including for consistency and avoid potential issues.
Helpers
- junit-jupiter-api
- junit-jupiter-engine
- JUnit 5
- Maven dependencies
- build.gradle
- testing frameworks