- 
                Notifications
    You must be signed in to change notification settings 
- Fork 129
Home
Safari Books Online, https://www.safaribooksonline.com/
Ken Kousen
[email protected]
@kenkousen
This document is for notes that come up during class. It’s also an easy way to share code and other materials.
- 
Sample code: https://github.com/kousen/junit5_workshop 
- 
JUnit 5 User Guide: https://junit.org/junit5/docs/current/user-guide/ 
- 
Javadocs: https://junit.org/junit5/docs/current/api/overview-summary.html 
- 
JUnit 5 Samples: https://github.com/junit-team/junit5-samples 
Note: JUnit 5 requires Java 8+.
- IntelliJ
- 
Import the build.gradlefile- 
Use Openif an existing project is open, orImportotherwise
- 
Navigate to the build.gradlefile in the root of the project
- 
Accept all the defaults in the Import wizard 
 
- 
- Eclipse
- 
Create an Eclipse project and import it - 
From a command prompt, execute >gradlew cleanEclipse eclipse
- 
Use File → Open → General → Existing Projects Into Workspace → navigate to the root of the project and select it 
 
- 
The following works for both IDEs:
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'
}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- 
@Testnow inorg.junit.jupiter.api
- 
@Beforeis now@BeforeEach
- 
@Afteris now@AfterEach
- 
@BeforeClassis now@BeforeAll
- 
@AfterClassis 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)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, returnsvoid
- Supplier<T>
- 
zero input args, returns T
- Function<T,R>
- 
one input arg of type T, returnsR
- Predicate<T>
- 
one input arg of type T, returnsboolean
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.
- 
Comparison of JUnit 5 vs Spock (by Ben Muschko) http://bmuschko.com/blog/junit5-vs-spock-showdown/ 
- 
Eclipse JUnit 5 support https://www.eclipse.org/community/eclipse_newsletter/2017/october/article5.php