Skip to content

francescofavatella/java-junit5

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Introduction to junit5

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)

How to run tests

./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

Requirements

This project requires at least Java 8.

How to install junit5

Create a new gradle project

https://www.jetbrains.com/help/idea/getting-started-with-gradle.html

Add Junit5

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'
}

Add Mockito

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"
}

Add Cucumber

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']
        }
    }
}

Add Lombok (not required)

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'
}

How to use junit5

0. Platform

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.

1. Test instance lifecycle

@BeforeEach (@Before)
@AfterEach (@After)
@BeforeAll (@BeforeClass)
@AfterAll (@AfterClass)

2. Meta-Annotations and Composed Annotations

@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

3. Nested Classes

@Nested

4. Disable tests

@Disabled (@Ignore)
@DisplayName
@DisplayNameGeneration

Display name works only if you use intellij runner and in the html report.

5. Assertions

GroupedAssertions (assertAll)
Exceptions (assertThrows)
Timeout (assertTimeout)

6. Assumptions

assumeTrue
assumeThat

7. Conditional Test Execution

@EnabledIfSystemProperty 
@DisabledIfSystemProperty
@EnabledIfEnvironmentVariable
@EnabledIf
@DisabledIf

8. Dependency Injection for Constructors and Methods

TestInfo
Custom params
@ExtendWith
@RegisterExtension

9. Timeout Test

@Timeout

10. Dynamic Test Examples

@TestFactory

11. Parametrized Tests

@ParameterizedTest (@Theory)

12. Mockito integration

@ExtendWith(MockitoExtension.class)

13. Cucumber integration

@Cucumber

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors