SlideShare a Scribd company logo
Scott Leberknight
8/2/2018
JUnit
“…the next generation of JUnit…”
“…for Java 8 and beyond…”
“…enabling many different styles of testing”
Key Takeaways…
Next gen test framework…
New programming & extension model
for developers
Stable platform for tool providers
Migration path from earlier versions
Platform + Jupiter + Vintage
JUnit 5 =
JUnit Platform
Launcher for test frameworks on JVM
Defines TestEngine API
Console Launcher
Maven, Gradle plugins
JUnit Jupiter
New programming model
Standard assertions
Extension model
(replaces @Rules)
TestEngine for running Jupiter tests
JUnit Vintage
TestEngine to run
JUnit 3 & 4 tests
Eases migration
to Jupiter…
Dependencies
Separate dependency groups…
org.junit.platform
org.junit.jupiter
org.junit.vintage
junit-jupiter-api
junit-jupiter-engine
junit-jupiter-migrationsupport
junit-vintage-engine
…and more
common dependencies…
Writing tests
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
class FirstJupiterTest {
@Test
void theAnswer() {
assertEquals(42, 22 + 20);
}
}
Jupiter Basics
No need to be public
Annotation-based (like JUnit 4)
Assertions & Assumptions
@Test
@DisplayName("All Your Base Are Belong To Us™")
void allYourBase() {
assertAll(
() -> assertTrue(true),
() -> assertEquals("42", "4" + "2"),
() -> assertTimeout(ofSeconds(1), () -> 42 * 42)
);
}
@Test
@DisplayName("To ∞ & beyond...")
void toInfinity() {
Integer result = assertDoesNotThrow(() ->
ThreadLocalRandom.current().nextInt(42));
assertTrue(result < 42);
}
@Test
@Test indicates a test method
Test methods cannot be private or static
Test methods can declare parameters
Jupiter supports meta-annotations
@Slf4j
class FirstParameterTest {
@BeforeEach
void setUp(TestInfo info) {
LOG.info("Executing test: {} tagged with {}",
info.getDisplayName(), info.getTags());
}
@Test
@Tag("fast")
void multiply() {
assertEquals(42, 21 * 2);
}
}
( @Slf4j is a Lombok annotation that generates an SLF4J Logger )
Jupiter Annotations
Reside in org.junit.jupiter.api
Some common ones:
@BeforeEach, @AfterEach
@BeforeAll, @AfterAll
@DisplayName
@Tag
@Disabled
Jupiter Annotations
And some more exotic ones…
@Nested
@TestInstance
@TestFactory
@TestTemplate
@ParameterizedTest
@RepeatedTest
Meta-Annotations
@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Tag("fast")
@interface Fast {}
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Test
@Fast
public @interface FastTest {}
@FastTest
void lightning() {
assertFalse(false);
}
@TestInstance
Controls lifecycle of test instances
Per method is the default;
creates new instance for every test
Per class re-uses the same
instance for every test
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@Slf4j
class PerClassTestLifecycleTest {
private int sum = 0;
@Test
void add5() { sum += 5; }
@Test
void add10() { sum += 10; }
@AfterEach
void tearDown() {
LOG.info("The current sum is: {}", sum);
}
}
@TestInstance
In general, stick with the default
PER_METHOD lifecycle…
…and become aware of
differences (pitfalls?) in behavior
when using PER_CLASS
@Nested
Allows arbitrary levels of nesting
Group common sets of tests
Cleanly separate setup/teardown code
@DisplayName("An ArrayList")
class NestedExampleTest {
private ArrayList<String> list;
@Nested
@DisplayName(“when new”)
class WhenNew {
@BeforeEach void setUp() { list = new ArrayList<>(); }
@Test @DisplayName(“is empty”) void isEmpty() { ... }
@Nested
@DisplayName(“after adding an element”)
class AfterAddElement {
@BeforeEach void setUp() { list.add(“an item”); }
@Test @DisplayName(“is no longer empty”)
void isNotEmpty() { ... }
}
}
}
Nested test output
@ParameterizedTest
Execute tests multiple times with
different arguments
Requires a source of values
Currently an experimental feature!
class ParametersTest {
@ParameterizedTest
@ValueSource(ints = {2, 4, 6, 8, 10})
void evens(int value) {
assertEquals(0, value % 2,
() -> String.format("Expected %d mod 2 == 0", value));
}
@ParameterizedTest
@MethodSource("supplyOdds")
void odds(int value) {
assertEquals(1, value % 2);
}
private static Stream<Integer> supplyOdds() {
return Stream.of(1, 3, 5, 7, 9, 11);
}
}
Other value sources
Enums
CSV strings and files
Using ArgumentsProvider API
Dependency Injection
Constructors & methods can have parameters!
DIY using ParameterResolver API
Several built-in resolvers
(TestInfo, RepetitionInfo, and TestReporterParameterResolver)
@RepeatedTest(value = 10)
void repeatedTestWithInfo(RepetitionInfo repetitionInfo) {
assertTrue(repetitionInfo.getCurrentRepetition() <=
repetitionInfo.getTotalRepetitions());
assertEquals(10, repetitionInfo.getTotalRepetitions());
}
More…
Disabling & conditional execution
Tagging & filtering
Repeated tests
Assumptions
Test templates & dynamic tests
A word on Assertions
Jupiter provides the basics…
…and several more useful ones
But consider using 3rd party
assertion frameworks like AssertJ,
Hamcrest, or Google’s Truth
(asssertAll, assertTimeout, assertThrows, etc.)
Extension
Model
Extension API
Replaces JUnit 4 Runner, @Rule, & @ClassRule
Extension “marker” interface
Lifecycle callbacks define extension points
Constructor & method parameters
Registering extensions
Declarative via @ExtendWith
Programmatically (code + @RegisterExtension)
Java’s ServiceLoader mechanism
@ExtendWith
Apply to test classes & methods
Register multiple extensions
@ExtendWith(TempDirectory.class)
class FileExporterTest {
private Path tempDirectory;
@BeforeEach
void setUp(@TempDirectory.Root Path tempDir) {
this.tempDirectory = tempDir;
}
// ...
}
class ReportGeneratorTest {
@ExtendWith(TempDirectory.class)
@Test
void excelReport(@TempDirectory.Root Path tempDir) {
// ...
}
@Test
void clipboardReport() { ... }
@ExtendWith(TempDirectory.class)
@Test
void pdfReport(@TempDirectory.Root Path tempDir) {
// ...
}
}
@RegisterExtension
Programmatically construct extensions
Can register multiple extensions
class RandomThingsTest {
@RegisterExtension
final SoftAssertionsExtension softly =
new SoftAssertionsExtension();
@Test void addition() {
softly.assertThat(2 + 2).isEqualTo(4);
// ...
}
@Test void substrings() {
String str = "Quick brown fox jumped over the lazy dog";
softly.assertThat(str).contains("Quick");
softly.assertThat(str).contains(“brown");
softly.assertThat(str).contains(“lazy");
softly.assertThat(str).contains(“dog");
}
}
Extension lifecycle
BeforeAllCallback
BeforeEachCallback
BeforeTestExecutionCallback
AfterTestExecutionCallback
AfterEachCallback
AfterAllCallback
TestExecutionExceptionHandlerTest happens
here
(*)
A simple extension…
@Slf4j
public class SoftAssertionsExtension
extends SoftAssertions implements AfterEachCallback {
public SoftAssertionsExtension() {
LOG.trace("Constructed new instance");
}
public void afterEach(ExtensionContext context) {
LOG.trace("Asserting all soft assertions”);
assertAll();
}
}
SoftAssertions is an AssertJ class
More extension features
Automatic registration via ServiceLoader
Keeping state via ExtensionContext.Store
ParameterResolver for resolving parameters
Test instance post-processing
Conditional test execution
(disabled by default)
Migrating from JUnit 4…
No @Rules
Without any rules, it’s pretty easy!
…mostly changing imports
…and some annotations, e.g.
@Before to @BeforeEach
With @Rules
With existing rules, it depends…
Converting most rules is fairly easy
Many frameworks already have
JUnit 5 extensions
(e.g. Dropwizard, Spring, Mockito, …)
Migration Support
Must use @EnableRuleMigrationSupport
Includes support for:
ExternalResource
ExpectedException
Verifier (including ErrorCollector)
(*)
(*) this probably covers a lot of existing rules
APIs
Support API evolution over time
Mark public interface with @API status
Uses @APIGuardian for status, e.g.
STABLE, INTERNAL, EXPERIMENTAL
Wrap up
New programming & extension model
Allows migration over time and
keeping JUnit 4 & 5 in same project
Separates platform from test engines
Recommend use 3rd-party assertion library
sample code
available at:
https://github.com/sleberknight/junit5-presentation-code
References
JUnit 5 on GitHub
https://github.com/junit-team/junit5
@API Guardian
https://github.com/apiguardian-team/apiguardian
JUnit 5 web site
https://junit.org/junit5/
JUnit 5 User Guide
https://junit.org/junit5/docs/current/user-guide/
Dom Pérignon
https://www.flickr.com/photos/tromal/6989654843
https://creativecommons.org/licenses/by/2.0/
No changes made
Photos & Images
(All other images were purchased from iStock)
Jupiter Beer
https://www.flickr.com/photos/quinnanya/30680378305/
https://creativecommons.org/licenses/by-sa/2.0/
No changes made
My Info
sleberknight at
fortitudetec.com
www.fortitudetec.com
@sleberknight
scott.leberknight at
gmail

More Related Content

PPT
PDF
JUnit & Mockito, first steps
PPS
JUnit Presentation
PPTX
Java Unit Testing
PDF
Testing with JUnit 5 and Spring - Spring I/O 2022
PPT
05 junit
PPSX
PDF
Java 8 Stream API. A different way to process collections.
JUnit & Mockito, first steps
JUnit Presentation
Java Unit Testing
Testing with JUnit 5 and Spring - Spring I/O 2022
05 junit
Java 8 Stream API. A different way to process collections.

What's hot (20)

PDF
Mocking in Java with Mockito
PDF
An Introduction to JUnit 5 and how to use it with Spring boot tests and Mockito
PPTX
JUnit- A Unit Testing Framework
PPTX
JUNit Presentation
PPTX
TestNG Framework
PDF
JUnit 5 - The Next Generation
PPT
PPTX
Unit Testing in Java
PPTX
Unit Testing And Mocking
PDF
What is JUnit? | Edureka
PPTX
Unit Testing Concepts and Best Practices
PPTX
Introduction to JUnit
PPT
PDF
PDF
Java 8 features
PDF
Unit testing with JUnit
PDF
Java keywords
PDF
TypeScript - An Introduction
ODP
Why Katalon Studio?
PPT
Selenium
Mocking in Java with Mockito
An Introduction to JUnit 5 and how to use it with Spring boot tests and Mockito
JUnit- A Unit Testing Framework
JUNit Presentation
TestNG Framework
JUnit 5 - The Next Generation
Unit Testing in Java
Unit Testing And Mocking
What is JUnit? | Edureka
Unit Testing Concepts and Best Practices
Introduction to JUnit
Java 8 features
Unit testing with JUnit
Java keywords
TypeScript - An Introduction
Why Katalon Studio?
Selenium
Ad

Similar to JUnit 5 (20)

PPTX
JUnit 5 - The Next Generation of JUnit - Ted's Tool Time
PPTX
Migrating to JUnit 5
PPTX
Testing with Junit4
PDF
JUnit 4 Can it still teach us something? - Andrzej Jóźwiak - Kariera IT Łodź ...
PPTX
JUnit 5 - from Lambda to Alpha and beyond
PDF
Testing with JUnit 5 and Spring
PDF
junit-160729073220 eclipse software testing.pdf
PPTX
unit 1 (1).pptx
DOCX
Junit With Eclipse
PDF
What is new in JUnit5
PPTX
Introduction to JUnit testing in OpenDaylight
PPT
Junit and testNG
PDF
JUnit 5 — New Opportunities for Testing on the JVM
PDF
JUnit 5 - Evolution and Innovation - SpringOne Platform 2019
PPTX
Software Testing and JUnit and Best Practices
PPTX
Renaissance of JUnit - Introduction to JUnit 5
PPT
3 j unit
PPTX
Junit 5 - Maior e melhor
PPTX
JUnit 5 - The Next Generation of JUnit - Ted's Tool Time
Migrating to JUnit 5
Testing with Junit4
JUnit 4 Can it still teach us something? - Andrzej Jóźwiak - Kariera IT Łodź ...
JUnit 5 - from Lambda to Alpha and beyond
Testing with JUnit 5 and Spring
junit-160729073220 eclipse software testing.pdf
unit 1 (1).pptx
Junit With Eclipse
What is new in JUnit5
Introduction to JUnit testing in OpenDaylight
Junit and testNG
JUnit 5 — New Opportunities for Testing on the JVM
JUnit 5 - Evolution and Innovation - SpringOne Platform 2019
Software Testing and JUnit and Best Practices
Renaissance of JUnit - Introduction to JUnit 5
3 j unit
Junit 5 - Maior e melhor
Ad

More from Scott Leberknight (20)

PDF
JShell & ki
PDF
JUnit Pioneer
PDF
JDKs 10 to 14 (and beyond)
PDF
Unit Testing
PDF
PDF
AWS Lambda
PDF
Dropwizard
PDF
RESTful Web Services with Jersey
PDF
jps & jvmtop
PDF
Cloudera Impala, updated for v1.0
PDF
Java 8 Lambda Expressions
PDF
Google Guava
PDF
Cloudera Impala
PDF
Apache ZooKeeper
PDF
HBase Lightning Talk
PDF
wtf is in Java/JDK/wtf7?
PDF
CoffeeScript
JShell & ki
JUnit Pioneer
JDKs 10 to 14 (and beyond)
Unit Testing
AWS Lambda
Dropwizard
RESTful Web Services with Jersey
jps & jvmtop
Cloudera Impala, updated for v1.0
Java 8 Lambda Expressions
Google Guava
Cloudera Impala
Apache ZooKeeper
HBase Lightning Talk
wtf is in Java/JDK/wtf7?
CoffeeScript

Recently uploaded (20)

PDF
Review of recent advances in non-invasive hemoglobin estimation
PPTX
Cloud computing and distributed systems.
PPT
Teaching material agriculture food technology
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PPTX
Big Data Technologies - Introduction.pptx
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Machine learning based COVID-19 study performance prediction
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Electronic commerce courselecture one. Pdf
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PDF
Encapsulation_ Review paper, used for researhc scholars
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
Review of recent advances in non-invasive hemoglobin estimation
Cloud computing and distributed systems.
Teaching material agriculture food technology
“AI and Expert System Decision Support & Business Intelligence Systems”
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Chapter 3 Spatial Domain Image Processing.pdf
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Big Data Technologies - Introduction.pptx
Diabetes mellitus diagnosis method based random forest with bat algorithm
Machine learning based COVID-19 study performance prediction
Understanding_Digital_Forensics_Presentation.pptx
Electronic commerce courselecture one. Pdf
CIFDAQ's Market Insight: SEC Turns Pro Crypto
Encapsulation_ Review paper, used for researhc scholars
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
Reach Out and Touch Someone: Haptics and Empathic Computing
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Mobile App Security Testing_ A Comprehensive Guide.pdf

JUnit 5