DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Why Testing is a Long-Term Investment for Software Engineers
  • TestNG vs. JUnit: A Comparative Analysis of Java Testing Frameworks
  • Mastering Unit Testing and Test-Driven Development in Java
  • JUnit, 4, 5, Jupiter, Vintage

Trending

  • Unlocking AI Coding Assistants Part 1: Real-World Use Cases
  • Emerging Data Architectures: The Future of Data Management
  • Understanding Java Signals
  • Doris: Unifying SQL Dialects for a Seamless Data Query Ecosystem
  1. DZone
  2. Coding
  3. Java
  4. JUnit 5 Tutorial: Nice and Easy [Video]

JUnit 5 Tutorial: Nice and Easy [Video]

Want a fun and engaging intro to the JUnit 5 testing ecosystem? Check out this live-coding session on a new episode of "Marco Codes."

By 
Marco Behler user avatar
Marco Behler
DZone Core CORE ·
May. 11, 22 · Tutorial
Likes (5)
Comment
Save
Tweet
Share
5.6K Views

Join the DZone community and get the full member experience.

Join For Free

Ever looked for a comprehensive intro to JUnit 5 that is fun and entertaining at the same time? Then have a look at this brand-new episode of the "Marco Codes" YouTube channel: JUnit 5 Tutorial - Nice & Easy.

In this video, you'll learn how to use JUnit 5 like a professional. Understand what Maven/Gradle dependencies you'll need, how to write tests, how to use other assertion libraries like AssertJ and asserting JSON & XML, and explore new JUnit 5 features like @ParameterizedTests, @TestFactorys, and @ExtendWith. By the end of the tutorial, there won't be many questions left when it comes to JUnit 5.

What’s in the Video?

If you want to test something with JUnit 5, you'll obviously have to start with setting up the right Maven/Gradle dependencies unless you are already using a framework like Spring Boot, which does this automatically for you. Hence, this is how we'll start.

We'll then continue writing our first test. Most people by today know about the @Test annotation, but there are always many arguments to be had over consistent naming styles of your tests, so we'll cover those soft topics as well.

If you're not paying attention, you will be able to run JUnit 5 tests from inside your IDE, but not from the command line via Maven. There are tiny infrastructure problems you need to be aware of.

We'll then cover assertions. JUnit 5 comes with its own set of assertions, but there's actually a better way: Using a library called AssertJ in combination with JUnit 5. AssertJ offers fluent assertions, and you'll learn how to use those.

Even better, AssertJ integrates with other popular testing libraries to write assertions against XML and JSON files, which comes in handy in most real-life projects.

After having had your first successes with tests and assertions, we'll have a look at the JUnit 5 lifecycle, which means @BeforeAll, @BeforeEach, @AfterAll, @AfterEach. In most tests, you usually want to set something up before running them, or do clean-up afterward. Those annotations allow you to do just that.

Then comes the first big new feature of JUnit 5, which is the @ParameterizedTest annotation. It allows you to write just one test method that JUnit can duplicate on the fly based on an input list of values: from simple lists to CSV file rows to enum values, it's a super useful new feature!

You can take it even further with the @TestFactory annotation, which allows you to dynamically generate new test methods through code! We'll come up with the simplest example possible, generating new test methods on-the-fly for all XML files in your project's directory.

Sooner or later, people want to run their tests as part of a CI Pipeline. JUnit 5 offers the @Tag annotation for that, which lets you easily split up unit and integration tests, for example, so that later on, you can easily run them in isolation.

Last but not least is the @ExtendWith annotation. You might want to use JUnit 5 together with other libraries, such as Mockito or Spring Boot. The @ExtendWith annotation is the entry point for all the integrations with 3rd party frameworks.

If you managed to watch the entire video, you'll be presented with some hints, tips, and resources, that will help you continue your JUnit learning journey on your own.  Check it out and let us know what you think!  


JUnit

Opinions expressed by DZone contributors are their own.

Related

  • Why Testing is a Long-Term Investment for Software Engineers
  • TestNG vs. JUnit: A Comparative Analysis of Java Testing Frameworks
  • Mastering Unit Testing and Test-Driven Development in Java
  • JUnit, 4, 5, Jupiter, Vintage

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!