JUnit 5 Tutorial

Last updated: November 14, 2023

My JUnit 5 tutorial helps you get started with JUnit 5, describes how you can write automated tests for Spring and Spring Boot web applications, and helps you to write tests for code which uses external HTTP / REST APIs.

If you want to get access to up-to-date material which helps you to work smarter and save time when you are writing tests with JUnit 5, you should take a look at my Introduction to JUnit 5 course. It has 24 lessons, 47 exercises, and 13 quizzes.

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.
  • Introduction to JUnit 5 Test Classes describes how you can create a new test class with JUnit 5, helps you to understand how you can use setup and teardown methods, and explains how you can add test method to your test class. Note that this post assumes that you have no previous experience from JUnit 4.
  • Writing Nested Tests With JUnit 5 describes how you can create nested test classes with JUnit 5 and explains how you can add setup, teardown, and test methods to nested test classes.
  • Introduction to JUnit 5 Tags describes how you can add tags to your test methods.
  • Filtering JUnit 5 Tests With Maven and Gradle describes how you can identify test methods with tag expressions and filter JUnit 5 tests with Maven and Gradle.
  • Writing Parameterized Tests With JUnit 5 describes how you can write parameterized tests with JUnit 5 and configure the arguments which are passed to your parameterized test methods.
  • 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 JUnit 5 and 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 JUnit 5 and 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.
  • Introduction to JUnit 5 Extensions describes what a JUnit 5 extension is, helps you to understand when JUnit 5 extensions are run, and explains how you can register JUnit 5 extensions.

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:

Writing Integration Tests for Spring Boot Web Applications

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.