JUnit 5 Tutorial

This JUnit 5 tutorial teaches us how to write and run tests for our Java and Kotlin code. The tutorial targets both beginners and people already familiar with some version of JUnit.

The articles in the tutorial have been broken down to different sections.

Getting Started

First section teaches all the basics of JUnit 5, from the setup to making assertions, and how the test lifecycle works.

  • Writing Your First Test - Learn to write your first JUnit 5 test. Start here if you are completely new to testing.

  • Assertions - Learn to write JUnit 5 assertions to verify tests results.

  • Parameterized tests – Learn to remove duplication from test code by writing JUnit 5 parameterized tests. Use different sources of parameters to run the same test several times using different input.

  • Maven Example – Configure Maven dependencies for writing JUnit 5 tests and Maven Surefire plugin for running them.

  • Gradle Example – Configure Gradle dependencies for writing JUnit 5 tests and JUnit Gradle plugin for running them.

Advanced

This section consists of advanced topics like how to use different testing libraries with JUnit 5. The section consists of the following articles:

Also, if you are already familiar with JUnit 4, you might be interested in:

  • Migrating from JUnit 4 to JUnit 5 – Learn the steps required for migrating from JUnit 4 to JUnit 5. Run your existing JUnit 4 tests along with JUnit 5, or migrate the code to the new version.

Frequently Asked Questions