JUnit 5 Tutorial

Last updated: May 9, 2022

This page contains several tutorials which help you to write automated tests with JUnit 5.

These tutorials uses JUnit 5.8.2

Introduction to JUnit 5

If you want to get started with JUnit 5, you should take a look at these blog posts:

  • Running Unit Tests With Maven describes how you can get the required dependencies with Maven and create a Maven project that runs your unit tests.
  • Running Unit Tests With Gradle describes how you can get the required dependencies with Gradle and create a Gradle project that runs your unit tests.
  • Writing Our First Test Class describes how you can use setup and teardown methods, and add test method to your test class. Note that this post assumes that you have no previous experience from JUnit 4.
  • Writing Nested Tests describes how you can write nested tests with JUnit 5, and leverage setup and teardown methods in your nested tests.
  • Writing Parameterized Tests describes how you can write parameterized tests with JUnit 5.
  • Writing Assertions With JUnit 5 Assertion API describes how you can write assertions with JUnit 5 assertion API, provide a custom error message that is shown if an assertion fails, and group assertions with JUnit 5.
  • Writing Assertions With Hamcrest describes how you can get the required dependencies with Maven and Gradle, write basic assertions with Hamcrest, combine multiple Hamcrest matchers, and provide a custom error message that is shown when an assertion fails.
  • Writing Assertions With AssertJ describes how you can get the required dependencies with Maven and Gradle, write basic assertions with AssertJ, write soft assertions with AssertJ, and provide a custom error message that is shown when an assertion fails.

Writing Unit Tests for Spring MVC Controllers

If you want to write unit tests for Spring MVC controller methods, you should take a look at these blog posts:

WireMock Tutorial

If you want to write automated tests for code which invokes external HTTP APIs, you should take a look at these blog posts:

  • Introduction provides a quick introduction to WireMock. After you have read this blog post, you can identify the key features WireMock, you understand when you should use it, and you can get the required dependencies with Maven and Gradle.
  • Configuration describes how you can configure WireMock from the scratch. After you have read this blog post, you can configure the used WireMock server, you know how you can start the WireMock server before a test method is run, and you can stop the WireMock server after a test method has been run.
  • Request Matching, Part One provides an introduction the request matching support of WireMock. This blog post helps you to understand how request matching works, and describes how you can specify expectations for request methods and request urls.
  • Request Matching, Part Two describes how you can use the WireMock's request matching support when you want specify expectations for cookies, HTTP headers, and request parameters.
  • Request Matching, Part Three describes how you can specify expectations for the JSON document that is received by your WireMock server.
  • Request Matching, Part Four describes how you can specify expectations for the XML document that is received by your WireMock server.
  • Introduction to Stubbing describes how you can ensure that your WireMock server returns a configured HTTP response when it receives a specific HTTP request.