®
5
JUNIT 5
THE NEW TESTING FRAMEWORK FOR JAVA 
AND PLATFORM FOR THE JVM
®
5
MARC PHILIPP
So ware Engineer at 
JUnit Maintainer since 2012
Twi er:   
Web: 
@marcphilipp
marcphilipp.de
®
5
✋
SHOW OF HANDS
®
5
 
JUNIT 5 IS HERE!
5.0 
September 10, 2017
5.1 
February 18, 2018
5.2 
April 29, 2018
®
5
AGENDA
1. How to write tests and extensions using JUnit 5?
2. What is the JUnit Pla orm and why do we need it?
3. What’s s ll to come and how to get started?
®
5
JUNIT JUPITERJUNIT JUPITERJUNIT JUPITERJUNIT JUPITERJUNIT JUPITER
THE NEW TESTING FRAMEWORK FOR JAVATHE NEW TESTING FRAMEWORK FOR JAVATHE NEW TESTING FRAMEWORK FOR JAVATHE NEW TESTING FRAMEWORK FOR JAVATHE NEW TESTING FRAMEWORK FOR JAVA
Image: NASA
®
5
JUPITER? 
Nope, it’s just a new name so we can easily dis nguish it
from the old JUnit and the other parts of JUnit 5. 
… and it’s the fi h planet from the sun. 
Is wri ng tests rocket science now?
®
5
BASICS (DEMO)
h ps://github.com/marcphilipp/junit5‑
demo/tree/20180425‑jax
®
5
BASICS (RECAP)
 @Test  is s ll a thing, but in  org.junit.jupiter.api 
 @Disabled  instead of  @Ignore 
 @BeforeAll ,  @BeforeEach ,  @AfterEach ,  @AfterAll 
have new names
 Assertions  look similar – a few new ones like
 assertThrows ,  assertAll 
Custom  @DisplayNames  to ease camel‑case fa gue
 @TestInstance(PER_METHOD  or  PER_CLASS) 
 @Tag  instead of  @Category 
®
5
MORE WAYS TO TEST (DEMO)
h ps://github.com/marcphilipp/junit5‑
demo/tree/20180425‑jax
®
5
MORE WAYS TO TEST (RECAP)
 @ParameterizedTest  with different  @Source 
annota ons
 @ValueSource ,  @EnumSource ,  @CsvSource ,
 @CsvFileSource ,  @MethodSource ,
 @ArgumentsSource(MyProvider.class) ,
 @YourCustomSource 
 @RepeatedTest  for flaky tests
 @TestFactory  to produce dynamic tests
®
5
EXTENSIONS (DEMO)
h ps://github.com/marcphilipp/junit5‑
demo/tree/20180425‑jax
®
5
EXTENSIONS (RECAP)
Registra on (as many as you need):
Declara ve:  @ExtendWith  on classes or methods
Programma c:  @RegisterExtension  on fields
Global: Via  ServiceLoader  (see  )
Implementa on:
 Extension  marker interface
one extension – n extension points/interfaces
User Guide
®
5
COMPOSED ANNOTATIONS
You can use Jupiter annota ons as meta‑annota ons to
create your own annota ons.
@Retention(RUNTIME)
@Target(METHOD)
@ExtendWith(ConferenceExecutionCondition.class)
@Tag("example")
public @interface DisabledOnConference {}
®
5
EXTENSION POINTS
Lifecycle:  BeforeAllCallback ,  BeforeEachCallback ,
 BeforeTestExecutionCallback ,
 TestExecutionExceptionHandler ,
 AfterTestExecutionCallback ,  AfterEachCallback ,
 AfterAllCallback 
Other:  ExecutionCondition ,
 TestInstancePostProcessor ,  ParameterResolver ,
 TestTemplateInvocationContextProvider 
®
5
THIRD‑PARTY EXTENSIONS
Spring, Mockito, Docker, Wiremock, JPA,
Selenium/WebDriver, DbUnit, Ka a, Jersey, GreenMail,
S3Mock, Citrus Framework, XWiki, …
h ps://github.com/junit‑team/junit5/wiki/Third‑party‑
Extensions
®
5
AGENDA
1. How to write tests and extensions using JUnit 5? ✅
2. What is the JUnit Pla orm and why do we need it?
3. What’s s ll to come and how to get started?
®
5
JUNIT PLATFORMJUNIT PLATFORMJUNIT PLATFORMJUNIT PLATFORMJUNIT PLATFORM
PLATFORM FOR TESTING ON THE JVMPLATFORM FOR TESTING ON THE JVMPLATFORM FOR TESTING ON THE JVMPLATFORM FOR TESTING ON THE JVMPLATFORM FOR TESTING ON THE JVM
Image: NASA
®
5
JUNIT AS A PLATFORM?
JUnit has always been a pla orm
for IDEs and build tools
for other tes ng frameworks
Closely coupled (internal APIs, reflec on, serializa on)
 
®
5
COUPLING
 
®
5
If JUnit is a pla orm, let’s design for it!
®
5
SEPARATION OF CONCERNS
1. An API to write tests and extensions (Jupiter API)
2. Extensible mechanisms to discover and execute tests (Test
Engine SPI)
3. An API for test execu on by tools (Launcher API)
®
5
DESIGN GOALS
Flexibility: Adding new features should be easy. It should
be clear whether a change might poten ally break a client.
Backward Compa bility: Test wri en with JUnit 3 and 4
should s ll run
Forward Compa bility: Old IDEs and build tools should be
able to execute new tests
®
5
JUNIT5
P L AT F O R M
Another Test
P A R T Y
T H I R D
J U P I T E RV I N TA G E
 
®
5
JUNIT 5
=
JUPITER + VINTAGE + PLATFORM
®
5
THIRD‑PARTY ENGINES
Specsy, Spek, KotlinTest, Cucumber, Drools, jqwik, …
h ps://github.com/junit‑team/junit5/wiki/Third‑party‑
Extensions
Talk about jqwik by   
 11:45 in Gutenberg 1 
@johanneslink
®
5
COMPATIBILITY / MIGRATION
Vintage Engine to run JUnit 3/4 tests on the Pla orm
 @Category(UI.class)  maps to  com.acme.UI  tag
Limited support for JUnit 4  Rules  to ease migra on
IDEs provide tools to convert test classes to Jupiter API
Community‑provided migra on tool: 
h ps://github.com/boyarsky/convert‑junit4‑to‑junit5
®
5
BUILD TOOL SUPPORT
Na ve support in Gradle (≥ 4.6) and Ant (≥ 1.10.3)
Support for Maven Surefire/Failsafe via a custom provider
(currently being donated to Apache)
 ConsoleLauncher  to run tests from the command line
®
5
IDE SUPPORT
Excellent support
IntelliJ IDEA (≥ 2016.2)
Eclipse (≥ 4.7.1a)
Visual Studio Code (Java Test Runner ≥ 0.4.0)
For other tools, there’s  @RunWith(JUnitPlatform) 
®
5
USING MULTIPLE ENGINES (DEMO)
h ps://github.com/marcphilipp/junit5‑pla orm‑demo
®
5
USING MULTIPLE ENGINES (RECAP)
Mul ple test engines can be used in a single test run
Dis nc on between  testCompile  and  testRuntime 
dependencies
Allows to gradually migrate tests from one test engine to
another (e.g. from Vintage to Jupiter)
®
5
AGENDA
1. How to write tests and extensions using JUnit 5? ✅
2. What is the JUnit Pla orm and why do we need it? ✅
3. What’s s ll to come and how to get started?
®
5
ROADMAP ANDROADMAP ANDROADMAP ANDROADMAP ANDROADMAP AND
RESOURCESRESOURCESRESOURCESRESOURCESRESOURCES
Image: NASA
®
5
IMPORTANT FUTURE MILESTONES
Parallel test execu on
Release of donated provider for Apache Maven Surefire
New repor ng format to support new features
Parameterized Classes
Scenario Tests
Your ideas?
®
5
GETTING STARTED
User Guide: 
Sample projects for Gradle, Maven, and Ant: 
Javadoc: 
h p://junit.org/junit5/docs/current/user‑guide/
h ps://github.com/junit‑team/junit5‑samples
h p://junit.org/junit5/docs/current/api/
®
5
WANTED: FEEDBACK!
StackOverflow: 
Code & Issues: 
Twi er: 
h p://stackoverflow.com/ques ons/tagged/junit5
h ps://github.com/junit‑team/junit5/
h ps://twi er.com/juni eam
®
5
EXAMPLE CODE
Jupiter: 
Pla orm: 
h ps://github.com/marcphilipp/junit5‑
demo/tree/20180425‑jax
h ps://github.com/marcphilipp/junit5‑pla orm‑demo
®
5
QUESTIONS?
 
   or @marcphilipp @juni eam
®
5
THANKS!

JUnit 5: The new Testing Framework for Java and Platform for the JVM