This repository contains code examples of JUnit 5 and the integration with third-party frameworks and technologies like:
- Mockito (popular mock framework)
- Cucumber (popular BDD style framework)
./gradlew test runs all the junit tests
./gradlew cucumber runs all the cucumber tests
./gradlew test --tests com.example.<YOUR_CLASS_NAME> runs a specific test class
This project requires at least Java 8.
https://www.jetbrains.com/help/idea/getting-started-with-gradle.html
https://www.baeldung.com/junit-5-gradle
https://junit.org/junit5/docs/current/user-guide/
repositories {
mavenCentral()
}
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.0'
testCompileOnly 'junit:junit:4.12'
testRuntimeOnly 'org.junit.vintage:junit-vintage-engine:5.7.0'
testImplementation 'org.junit.jupiter:junit-jupiter-params:5.7.0'
}
https://www.baeldung.com/mockito-junit-5-extension
repositories {
jcenter()
}
dependencies {
testRuntimeOnly "org.mockito:mockito-core:2.23.0"
testCompileOnly "org.mockito:mockito-junit-jupiter:2.23.0"
}
https://cucumber.io/docs/tools/java/
repositories {
jcenter()
}
dependencies {
testImplementation 'io.cucumber:cucumber-java:6.9.0'
testImplementation 'io.cucumber:cucumber-junit-platform-engine:6.9.0'
}
configurations {
cucumberRuntime {
extendsFrom testImplementation
}
}
task cucumber() {
dependsOn assemble, testClasses
doLast {
javaexec {
main = "io.cucumber.core.cli.Main"
classpath = configurations.cucumberRuntime + sourceSets.main.output + sourceSets.test.output
args = ['--plugin', 'pretty', '--glue', 'com.example.cucumber', 'src/test/resources']
}
}
}
https://projectlombok.org/setup/gradle
repositories {
mavenCentral()
}
dependencies {
compileOnly 'org.projectlombok:lombok:1.18.16'
annotationProcessor 'org.projectlombok:lombok:1.18.16'
testCompileOnly 'org.projectlombok:lombok:1.18.16'
testAnnotationProcessor 'org.projectlombok:lombok:1.18.16'
}
JUNIT 5 = platform + jupiter + vintage
The JUnit Platform serves as a foundation for launching testing frameworks on the JVM. It also defines the TestEngine API for developing a testing framework that runs on the platform. Furthermore, the platform provides a Console Launcher to launch the platform from the command line and a JUnit 4 based Runner for running any TestEngine on the platform in a JUnit 4 based environment. First-class support for the JUnit Platform also exists in popular IDEs (see IntelliJ IDEA, Eclipse, NetBeans, and Visual Studio Code) and build tools (see Gradle, Maven, and Ant).
JUnit Jupiter is the combination of the new programming model and extension model for writing tests and extensions in JUnit 5. The Jupiter sub-project provides a TestEngine for running Jupiter based tests on the platform.
JUnit Vintage provides a TestEngine for running JUnit 3 and JUnit 4 based tests on the platform.
@BeforeEach (@Before)
@AfterEach (@After)
@BeforeAll (@BeforeClass)
@AfterAll (@AfterClass)
@Tag
Composed annotation
Gradle Task to use tags
TODO: Check how to pass tags in gradlew
task slowTest(type: Test) {
useJUnitPlatform {
includeTags 'slow'
}
}
./gradlew slowTest
@Nested
@Disabled (@Ignore)
@DisplayName
@DisplayNameGeneration
Display name works only if you use intellij runner and in the html report.
GroupedAssertions (assertAll)
Exceptions (assertThrows)
Timeout (assertTimeout)
assumeTrue
assumeThat
@EnabledIfSystemProperty
@DisabledIfSystemProperty
@EnabledIfEnvironmentVariable
@EnabledIf
@DisabledIf
TestInfo
Custom params
@ExtendWith
@RegisterExtension
@Timeout
@TestFactory
@ParameterizedTest (@Theory)
@ExtendWith(MockitoExtension.class)
@Cucumber