SlideShare a Scribd company logo
5
Most read
8
Most read
18
Most read
An introduction to JUnit 5
&
How to use it with Spring tests and Mockito
Shaun Thomas
Shaunthomas999[at]gmail.com
JUnit 4
• Released 2006 (more than a decade ago)
• Made use of Java 5 APIs
• Fat jar – junit.jar
• Supports JUnit 3
JUnit 5 - Overview
JUnit 5
• Released in 2017
• Works only with Java version >= 8
• JUnit 5 = Platform + Jupiter + Vintage
• Supports JUnit 4 and so also JUnit 3
• Good to use with - maven-surefire-plugin >= 2.22.0
Components Overview
Third partyVintage Jupiter
Platform
Old tests New tests
Third party
tests
Build tools/IDEs
Foundation for discovering, filtering,
configuring and executing tests
• Launcher API – build tools & IDE
• TestEngine API
• ConsoleLauncher
TestEngine and APIs for:
• Writing Tests
• New annotations & methods
• Extensions
• Extension API
TestEngine for running JUnit 4 and
JUnit 3 based tests on the platform
Third party frameworks runs on
platform by making use of TestEngine
API
Module dependencies
JUnit 5 - Features
Refer to project
https://github.com/shaunthomas999/junit5-
samples/tree/master/junit5-features
Assertions
• Standard assertions
• Grouped assertions
• Dependent assertions
• Can be used with Assertj, Hamcrest etc. libs
Assumptions
• Purpose
• Test only if certain resource is present
• Test only if environment variable has certain
value
• Profile based testing – Env., user etc.
• Contains subset of assumption methods from
JUnit 4
• New method assumingThat
• Throws AssumptionViolatedException to signal
that test has been aborted
Display name
• @DisplayName
• Description text
• Emoji 😱
Tags
• @Tag
• Class level / method level
• Can assign multiple tags to a class / method
• Filtering
• IDE level
• Command line
• -DincludeTags
• -DexcludeTags
• Surefire plugin - config
Create New
Test
Annotations
• Method / Class level
Repeated
Tests
• Execute tests specified number of times
• Can get info about current repetition/iteration
and total repetition by passing RepetitionInfo as
method parameter
• Option to customize display name for each
repetition
Parameterized
Tests
• Need additional dependency
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>${junit-jupiter.version}</version>
<scope>test</scope>
</dependency>
• Provide values through @ValueSource,
@EnumSource, @MethodSource, @CsvSource,
@CsvFileSource etc.
JUnit 5 with Spring
Tests & Mockito
Refer to project
https://github.com/shaunthomas999/junit5-
samples/tree/master/spring-boot-mockito-junit5
Test instance
Lifecycle
• Test class is instantiated at per test method
level by default (PER_METHOD)
• Same as Junit 4
• Even for @Disabled method test class is
instantiated
• Ways to change it:
• Class level instantiation -
@TestInstance(Lifecycle.PER_CLASS)
• JVM parameter –
-Djunit.jupiter.testinstance.lifecycle.default=per_class
• Prop. File
src/test/resources >
junit-platform.properties >
junit.jupiter.testinstance.lifecycle.default = per_class
Q & A
Thanks for your
attention J

More Related Content

PDF
JUnit & Mockito, first steps
PDF
PPSX
PPS
JUnit Presentation
PDF
Mocking in Java with Mockito
PDF
Testing with Spring: An Introduction
PPTX
Selenium WebDriver training
PPTX
Unit Testing And Mocking
JUnit & Mockito, first steps
JUnit Presentation
Mocking in Java with Mockito
Testing with Spring: An Introduction
Selenium WebDriver training
Unit Testing And Mocking

What's hot (20)

PPTX
Java Unit Testing
PDF
PPT
05 junit
PPTX
JUNit Presentation
PDF
JUnit 5 - The Next Generation
PPTX
Introduction to JUnit
PDF
An introduction to unit testing
PPTX
Unit Testing Concepts and Best Practices
PPT
PDF
Katalon Studio - Best automation solution for software testing team
PPTX
PDF
Testing with JUnit 5 and Spring - Spring I/O 2022
PDF
Spring Boot
PPT
Selenium ppt
PDF
Robot Framework Introduction
PDF
Testing Angular
PPTX
Introduction to React JS
PPTX
Introduction to Node.js
PDF
Katalon Studio - Successful Test Automation for both Testers and Developers
PDF
Loom Virtual Threads in the JDK 19
Java Unit Testing
05 junit
JUNit Presentation
JUnit 5 - The Next Generation
Introduction to JUnit
An introduction to unit testing
Unit Testing Concepts and Best Practices
Katalon Studio - Best automation solution for software testing team
Testing with JUnit 5 and Spring - Spring I/O 2022
Spring Boot
Selenium ppt
Robot Framework Introduction
Testing Angular
Introduction to React JS
Introduction to Node.js
Katalon Studio - Successful Test Automation for both Testers and Developers
Loom Virtual Threads in the JDK 19
Ad

Similar to An Introduction to JUnit 5 and how to use it with Spring boot tests and Mockito (20)

PPTX
JUnit 5 Slides: Next generation Framework for Testing
PDF
Developing Selenium tests with JUnit 5
PPTX
Renaissance of JUnit - Introduction to JUnit 5
PDF
JUnit 5 - New Opportunities for Testing on the JVM
PPTX
Automated Testing on Web Applications
PPTX
JUnit 5 - from Lambda to Alpha and beyond
PDF
KrishnaToolComparisionPPT.pdf
PDF
TestNG introduction
PDF
How To Use Selenium Successfully
PDF
Continuous Test Automation, by Richard Langlois P. Eng. and Yuri Pechenko.
PDF
JUnit 5 — New Opportunities for Testing on the JVM
PPTX
Getting Started with Test-Driven Development at Longhorn PHP 2023
PDF
What is new in JUnit5
PPTX
Unit Testng with PHP Unit - A Step by Step Training
PPT
Maven basic concept
PDF
JUnit5 Custom TestEngines intro - version 2020-06
PDF
JUnit 5 Alpha
PPTX
The Test way
PDF
How to use selenium successfully
PDF
L08 Unit Testing
JUnit 5 Slides: Next generation Framework for Testing
Developing Selenium tests with JUnit 5
Renaissance of JUnit - Introduction to JUnit 5
JUnit 5 - New Opportunities for Testing on the JVM
Automated Testing on Web Applications
JUnit 5 - from Lambda to Alpha and beyond
KrishnaToolComparisionPPT.pdf
TestNG introduction
How To Use Selenium Successfully
Continuous Test Automation, by Richard Langlois P. Eng. and Yuri Pechenko.
JUnit 5 — New Opportunities for Testing on the JVM
Getting Started with Test-Driven Development at Longhorn PHP 2023
What is new in JUnit5
Unit Testng with PHP Unit - A Step by Step Training
Maven basic concept
JUnit5 Custom TestEngines intro - version 2020-06
JUnit 5 Alpha
The Test way
How to use selenium successfully
L08 Unit Testing
Ad

Recently uploaded (20)

DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
KodekX | Application Modernization Development
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPTX
Cloud computing and distributed systems.
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Machine learning based COVID-19 study performance prediction
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Unlocking AI with Model Context Protocol (MCP)
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PDF
Approach and Philosophy of On baking technology
PDF
cuic standard and advanced reporting.pdf
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
The AUB Centre for AI in Media Proposal.docx
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
KodekX | Application Modernization Development
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
The Rise and Fall of 3GPP – Time for a Sabbatical?
Cloud computing and distributed systems.
NewMind AI Weekly Chronicles - August'25 Week I
Machine learning based COVID-19 study performance prediction
Per capita expenditure prediction using model stacking based on satellite ima...
Digital-Transformation-Roadmap-for-Companies.pptx
Unlocking AI with Model Context Protocol (MCP)
Understanding_Digital_Forensics_Presentation.pptx
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
CIFDAQ's Market Insight: SEC Turns Pro Crypto
Approach and Philosophy of On baking technology
cuic standard and advanced reporting.pdf
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
“AI and Expert System Decision Support & Business Intelligence Systems”
Mobile App Security Testing_ A Comprehensive Guide.pdf

An Introduction to JUnit 5 and how to use it with Spring boot tests and Mockito