Skip to content
Ken Kousen edited this page May 16, 2019 · 5 revisions

JUnit 5

Safari Books Online, https://www.safaribooksonline.com/

This document is for notes that come up during class. It’s also an easy way to share code and other materials.

Note: JUnit 5 requires Java 8+.

Import GitHub repository into IDE

IntelliJ

Import the build.gradle file

  1. Use Open if an existing project is open, or Import otherwise

  2. Navigate to the build.gradle file in the root of the project

  3. Accept all the defaults in the Import wizard

Eclipse

Create an Eclipse project and import it

  1. From a command prompt, execute >gradlew cleanEclipse eclipse

  2. Use File → Open → General → Existing Projects Into Workspace → navigate to the root of the project and select it

Gradle build file

The following works for both IDEs:

build.gradle
plugins {
    id 'java'
    id 'eclipse'
}

group 'com.oreilly'
version '1.0-SNAPSHOT'

sourceCompatibility = 1.8

repositories {
    mavenCentral() // or jcenter()
}

test {
    useJUnitPlatform()
}

dependencies {
    testImplementation 'org.junit.jupiter:junit-jupiter:5.4.2'
}

To run JUnit 4 tests using JUnit 5:

test {
    useJUnitPlatform {
        includeEngines 'junit-vintage'
        excludeEngines 'junit-jupiter'
    }
}

dependencies {
    testImplementation 'junit:junit:4.12'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.4.2'
}

Setting a Gradle proxy

If you have a proxy to configure, create a file called gradle.properties in either the project root directory or in ~/.gradle (create that folder if necessary) and add:

systemProp.http.proxyHost=www.somehost.org
systemProp.http.proxyPort=8080
systemProp.http.proxyUser=userid
systemProp.http.proxyPassword=password
systemProp.http.nonProxyHosts=*.nonproxyrepos.com|localhost

systemProp.https.proxyHost=www.somehost.org
systemProp.https.proxyPort=8080
systemProp.https.proxyUser=userid
systemProp.https.proxyPassword=password
systemProp.https.nonProxyHosts=*.nonproxyrepos.com|localhost

Moving from JUnit 4 to JUnit 5

  • @Test now in org.junit.jupiter.api

  • @Before is now @BeforeEach

  • @After is now @AfterEach

  • @BeforeClass is now @BeforeAll

  • @AfterClass is now @AfterAll

Test instances can now be per method or per class. The default is per method (like in JUnit 4), but the default can be changed with

@TestInstance(TestInstance.Lifecycle.PER_CLASS)

Functional features

Java 8+ defines a set of functional interfaces (interfaces that have a single abstract method) in the java.util.function package.

Consumer<T>

one input arg of type T, returns void

Supplier<T>

zero input args, returns T

Function<T,R>

one input arg of type T, returns R

Predicate<T>

one input arg of type T, returns boolean

There are primitive variations involving int, long, and double for each (like IntConsumer, LongConsumer, and DoubleConsumer), and some have binary variations as well (like BiConsumer<T,U>, which takes two args and returns void).

None of the single abstract methods inside them throw a checked exception. JUnit 5 wanted to allow for exceptions, so they defined their own:

public interface Executable {
    void execute() throws Throwable
}

public interface ThrowingSupplier<T> {
    T get() throws Throwable
}

Executable is basically a Runnable (no input args, no return value) that throws Throwable. The framework needs to catch the exception. Likewise, ThrowingSupplier is a Supplier that is allowed to throw a Throwable.

References