SlideShare a Scribd company logo
kgolev.com@kotseto
JUnit 5
Kostadin Golev
CTO @ Rewards Labs
The Next Generation
kgolev.com@kotseto
kgolev.com@kotseto
Why JUnit 5?
Where Is It?
Writing Tests
Platform For The JVM
Running Along JUnit 3 & 4
Important New Features
IDE & Tool Support
kgolev.com@kotseto
kgolev.com@kotseto
Issues with JUnit 4
kgolev.com@kotseto
kgolev.com@kotseto
How bad?
Let’s rename some private variables to find out!
kgolev.com@kotseto
kgolev.com@kotseto
4.11:
org.junit.ComparisonFailure:
expected:<[1]L> but was:<[2]L>
assertEquals(1, 2)
kgolev.com@kotseto
4.12-beta-1:
org.junit.ComparisonFailure:
expected: null<null> but was: null<null>
assertEquals(1, 2)
kgolev.com@kotseto
Extension mechanism
Runner
@RunWith(SpringJUnit4ClassRunner.class)
Rule
@Rule ExpectedException thrown = none()
kgolev.com@kotseto
Powerful Composable
Runner Rule
kgolev.com@kotseto
Extension mechanism
Powerful Composable?
Runner Rule
kgolev.com@kotseto
kgolev.com@kotseto
Why JUnit 5?
Where Is It?
Writing Tests
Platform For The JVM
Running Along JUnit 3 & 4
Important New Features
IDE & Tool Support
kgolev.com@kotseto
Will this work?
kgolev.com@kotseto
There is no single JUnit 5
(JAR)
kgolev.com@kotseto
Not one big fat jar
We have more then ten now, in three groups
kgolev.com@kotseto
JUnit Platform
+
JUnit Jupiter
+
JUnit Vintage
JUnit 5
kgolev.com@kotseto
JUnit Platform
kgolev.com@kotseto
JUnit Jupiter
JUnit Vintage
kgolev.com@kotseto
Why JUnit 5?
Where Is It?
Writing Tests
Platform For The JVM
Running Along JUnit 3 & 4
Important New Features
IDE & Tool Support
kgolev.com@kotseto
kgolev.com@kotseto
kgolev.com@kotseto
kgolev.com@kotseto
kgolev.com@kotseto
kgolev.com@kotseto
@Test
void exception() {
RuntimeException thrown =
assertThrows(RuntimeException.class, () ->
library.throwRuntimeException("message")
);
assertEquals("message", thrown.getMessage());
}
kgolev.com@kotseto
kgolev.com@kotseto
kgolev.com@kotseto
What about Runners and
Rules?
kgolev.com@kotseto
We wrote some tests!
How do we run them?
kgolev.com@kotseto
Why JUnit 5?
Where Is It?
Writing Tests
Platform For The JVM
Running Along JUnit 3 & 4
Important New Features
IDE & Tool Support
kgolev.com@kotseto
Some History
Or how JUnit 5 team discovered they were building a
platform
kgolev.com@kotseto
It started as a big fat jar
Then everything got split in small, focused modules
kgolev.com@kotseto
Launcher
<<interface>>
Engine
Engine Impl
Jupiter API
implements
JUnit Platform
JUnit Jupiter
kgolev.com@kotseto
Launcher
<<interface>>
Engine
JUnit Platform
kgolev.com@kotseto
<<interface>>
Engine
Engine Implementation
implements
JUnit Platform
JUnit Jupiter
kgolev.com@kotseto
Jupiter API
JUnit Jupiter
@Test
@BeforeEach
assertEquals()
…
kgolev.com@kotseto
testRuntime “org.junit.jupiter:junit-jupiter-engine:5.0.0”
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<dependencies>
…
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.0.0</version>
</dependency>
</dependencies>
</plugin>
kgolev.com@kotseto
Why JUnit 5?
Where Is It?
Writing Tests
Platform For The JVM
Running Along JUnit 3&4
Important New Features
IDE & Tool Support
kgolev.com@kotseto
You can still run your
JUnit 3&4 tests
But it will not be JUnit 3&4 running them
kgolev.com@kotseto
Launcher
<<interface>>
Engine
Vintage Engine Impl
JUnit 3&4 API
implements
JUnit Platform
JUnit Vintage
kgolev.com@kotseto
JUnit Platform
JUnit Jupiter JUnit Vintage
JUnit5 tests JUnit4 tests
kgolev.com@kotseto
testRuntime “org.junit.jupiter:junit-jupiter-engine:5.0.0”
testRuntime “org.junit.vintage:junit-vintage-engine:5.0.0”
kgolev.com@kotseto
Platform for the JVM
Many Developers implemented their own engines,
reusing JUnit5 tool integration
kgolev.com@kotseto
Not only for Java
Test Engine implementations exist for
Scala, Kotlin and Groovy
kgolev.com@kotseto
Why JUnit 5?
Where Is It?
Writing Tests
Platform For The JVM
Running Along JUnit 3 & 4
Important New Features
IDE & Tool Support
kgolev.com@kotseto
assertAll(…)
kgolev.com@kotseto
assertEquals(1, 2);
assertEquals("String", "Another String");
org.opentest4j.AssertionFailedError:
Expected :1
Actual :2
kgolev.com@kotseto
assertAll(
() -> assertEquals(1, 2),
() -> assertEquals("String", “Another String")
)
org.opentest4j.MultipleFailuresError: Multiple Failures (2 failures)
expected: <1> but was: <2>
expected: <String> but was: <Another String>
kgolev.com@kotseto
What about Runners and
Rules (again)?
JUnit5 Extension model
kgolev.com@kotseto
@ExtendWith
kgolev.com@kotseto
kgolev.com@kotseto
Composable & Powerful
Use as many as you want
whenever you want them
kgolev.com@kotseto
@ExtendWith(SpringExtension.class)
@ExtendWith(MockitoExtension.class)
class SpringTest {
@Autowired
SpringComponent component;
@Mock
Dependency mocked;
kgolev.com@kotseto
class ParameterInTestMethods {
@Test
@ExtendWith(MockitoExtension.class)
void test(@Mock Dependency mocked) {
…
}
kgolev.com@kotseto
kgolev.com@kotseto
public class MockitoExtension implements
TestInstancePostProcessor {
@Override
public void postProcessTestInstance(
Object testInstance,
ExtensionContext context) {
MockitoAnnotations.initMocks(testInstance);
}
}
kgolev.com@kotseto
Some official extensions
SpringExtension - since Spring 5.0 (Sep 2017)
MockitoExtension - since 2.17.0 (Mar 2018)
kgolev.com@kotseto
@Nested
@DisplayName
kgolev.com@kotseto
public class Library {
private Books books;
public Library(Books books) {
this.books = books;
}
public void addBook(Book book) {
// do something with books
}
}
kgolev.com@kotseto
@Test
void whenBookExistsThenIncrementAmount() {}
@Test
void whenBookExistsThenCheckAmountMoreThenN() {}
@Test
void whenBookDoesNotExistThenCreateBook() {}
@Test
void whenBookDoesNotExistThenSendNewBookNotification() {}
kgolev.com@kotseto
kgolev.com@kotseto
kgolev.com@kotseto
kgolev.com@kotseto
Tests are often a tree,
not a list
kgolev.com@kotseto
kgolev.com@kotseto
@DisplayName("Book is added to library")
class BookAddedTest {


@DisplayName("when book exists")
@Nested
class whenBookExists {
@DisplayName("amount++")
@Test
void incrementAmount() {}
@DisplayName("check amount > limit")
@Test
void checkAmountMoreThenLimit() {}
}
. . .
}
kgolev.com@kotseto
kgolev.com@kotseto
@ParameterizedTest
kgolev.com@kotseto
testCompile “org.junit.jupiter:junit-jupiter-params:5.0.0"
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>5.0.0</version>
<scope>test</scope>
</dependency>
WARNING!
WARNING!
WARNING!
kgolev.com@kotseto
@Test
void strIsLessThenTenChar() {
int value = "str".length();
assertTrue(value < 10);
}
@Test
void StringIsLessThenTenChar() {
int value = "String".length();
assertTrue(value < 10);
}
kgolev.com@kotseto
kgolev.com@kotseto
kgolev.com@kotseto
kgolev.com@kotseto
@ValueSource(strings = {“str”, “String”})
@ValueSource(ints = {1, 3, 7, 9})
@EnumSource(SomeEnum.class)
@CsvSource({"1, 1", "2, 4", "4, 16”})
@CsvFileSource(resources=“testData.csv”)
kgolev.com@kotseto
@MethodSource(names=“stringAndIntProvider")
…
static Stream<Arguments> stringAndIntProvider() {
return Stream.of(
ObjectArrayArguments.create("foo", 3),
ObjectArrayArguments.create("foobar", 6)
);
}
kgolev.com@kotseto
enum Status {
OK, INVALID_NAME, INVALID_DATE, …
}
@ParameterizedTest
@EnumSource(value = Status.class, mode = EXCLUDE,
names = “OK")
void should_return_error_when_not_OK(Status status) {
…
kgolev.com@kotseto
kgolev.com@kotseto
Why JUnit 5?
Where Is It?
Writing Tests
Platform For The JVM
Running Along JUnit 3 & 4
Important New Features
IDE & Tool Support
kgolev.com@kotseto
Also supported pre-GA releases
Since Oxygen 4.7.1
kgolev.com@kotseto
Surefire provider
Not native yet
Native support since 4.6
Plugin
kgolev.com@kotseto
testCompile "org.junit.jupiter:junit-jupiter-api:5.0.0"
testRuntime "org.junit.jupiter:junit-jupiter-engine:5.0.0"
testCompile "org.junit.jupiter:junit-jupiter-params:5.0.0"
…
test {
useJUnitPlatform {
includeEngines 'junit-jupiter', 'junit-vintage'
}
failFast = true
}
kgolev.com@kotseto
junit.org/junit5
kgolev.com@kotseto
Questions?
@kotseto
kgolev.com/talks/junit5

More Related Content

PDF
Test code that will not slow you down
PDF
JUnit 5 - The Next Generation
PDF
What is new in JUnit5
PPTX
TDD - Unit Testing
PPTX
Renaissance of JUnit - Introduction to JUnit 5
PDF
An Introduction to JUnit 5 and how to use it with Spring boot tests and Mockito
PDF
JUnit 5 - Evolution and Innovation - SpringOne Platform 2019
Test code that will not slow you down
JUnit 5 - The Next Generation
What is new in JUnit5
TDD - Unit Testing
Renaissance of JUnit - Introduction to JUnit 5
An Introduction to JUnit 5 and how to use it with Spring boot tests and Mockito
JUnit 5 - Evolution and Innovation - SpringOne Platform 2019

What's hot (20)

PPT
Unit Testing RPG with JUnit
PPT
RPG Program for Unit Testing RPG
PPTX
Interpreter RPG to Java
ODP
Automated testing in Python and beyond
 
PDF
Modern Python Testing
PPT
Google C++ Testing Framework in Visual Studio 2008
PPT
20111018 boost and gtest
ODT
Testing in-python-and-pytest-framework
DOCX
Test driven development and unit testing with examples in C++
PDF
Agile mobile
KEY
iOS Unit Testing
PDF
Quality of life through Unit Testing
ODP
Python unit testing
PPT
Presentation_C++UnitTest
PDF
Quick Tour to Front-End Unit Testing Using Jasmine
PPT
Working Effectively With Legacy Code
PDF
Software Engineering - RS3
PPTX
JUnit 5 - from Lambda to Alpha and beyond
PPTX
Quickly Testing Qt Desktop Applications
PPT
Google mock for dummies
Unit Testing RPG with JUnit
RPG Program for Unit Testing RPG
Interpreter RPG to Java
Automated testing in Python and beyond
 
Modern Python Testing
Google C++ Testing Framework in Visual Studio 2008
20111018 boost and gtest
Testing in-python-and-pytest-framework
Test driven development and unit testing with examples in C++
Agile mobile
iOS Unit Testing
Quality of life through Unit Testing
Python unit testing
Presentation_C++UnitTest
Quick Tour to Front-End Unit Testing Using Jasmine
Working Effectively With Legacy Code
Software Engineering - RS3
JUnit 5 - from Lambda to Alpha and beyond
Quickly Testing Qt Desktop Applications
Google mock for dummies
Ad

Similar to JUnit 5 (20)

PDF
Test Dependencies and the Future of Build Acceleration
PDF
JUnit5 and TestContainers
PPT
What's New in Groovy 1.6?
PDF
Rise of the Machines - Automate your Development
PDF
Advanced Java Testing
PPTX
Desenvolva plugins para o compilador do Java 8
PPT
Unit testing with Spock Framework
PPTX
Boost up your productivity with Kotlin - Liferay Symposium France 2018
PDF
What to expect from Java 9
PPTX
Junit_.pptx
PDF
Lightweight Java EE with MicroProfile
ODP
Java EE 6 & GlassFish v3: Paving path for the future
PPTX
Mock cli with Python unittest
PPTX
Junit5 brujug
PPTX
Java 7 and 8, what does it mean for you
PDF
Industrial Strength Groovy - Tools for the Professional Groovy Developer: Pau...
PPTX
OpenDaylight Developer Experience 2.0
PDF
Using java8 for unit testing while being backward compatible
PDF
Developing Selenium tests with JUnit 5
PDF
Unlocking the Power of Iteration
Test Dependencies and the Future of Build Acceleration
JUnit5 and TestContainers
What's New in Groovy 1.6?
Rise of the Machines - Automate your Development
Advanced Java Testing
Desenvolva plugins para o compilador do Java 8
Unit testing with Spock Framework
Boost up your productivity with Kotlin - Liferay Symposium France 2018
What to expect from Java 9
Junit_.pptx
Lightweight Java EE with MicroProfile
Java EE 6 & GlassFish v3: Paving path for the future
Mock cli with Python unittest
Junit5 brujug
Java 7 and 8, what does it mean for you
Industrial Strength Groovy - Tools for the Professional Groovy Developer: Pau...
OpenDaylight Developer Experience 2.0
Using java8 for unit testing while being backward compatible
Developing Selenium tests with JUnit 5
Unlocking the Power of Iteration
Ad

Recently uploaded (20)

PDF
Softaken Excel to vCard Converter Software.pdf
PPTX
CHAPTER 2 - PM Management and IT Context
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PDF
Digital Strategies for Manufacturing Companies
PDF
System and Network Administraation Chapter 3
PPT
Introduction Database Management System for Course Database
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PDF
Nekopoi APK 2025 free lastest update
PDF
System and Network Administration Chapter 2
PPTX
Introduction to Artificial Intelligence
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PPTX
Operating system designcfffgfgggggggvggggggggg
PDF
top salesforce developer skills in 2025.pdf
PPTX
ai tools demonstartion for schools and inter college
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
medical staffing services at VALiNTRY
PPTX
CHAPTER 12 - CYBER SECURITY AND FUTURE SKILLS (1) (1).pptx
Softaken Excel to vCard Converter Software.pdf
CHAPTER 2 - PM Management and IT Context
Design an Analysis of Algorithms II-SECS-1021-03
Digital Strategies for Manufacturing Companies
System and Network Administraation Chapter 3
Introduction Database Management System for Course Database
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
Nekopoi APK 2025 free lastest update
System and Network Administration Chapter 2
Introduction to Artificial Intelligence
Design an Analysis of Algorithms I-SECS-1021-03
Operating system designcfffgfgggggggvggggggggg
top salesforce developer skills in 2025.pdf
ai tools demonstartion for schools and inter college
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
How to Choose the Right IT Partner for Your Business in Malaysia
How to Migrate SBCGlobal Email to Yahoo Easily
Navsoft: AI-Powered Business Solutions & Custom Software Development
medical staffing services at VALiNTRY
CHAPTER 12 - CYBER SECURITY AND FUTURE SKILLS (1) (1).pptx

JUnit 5