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:
- Introduction to Spring MVC Test Framework provides a quick introduction to Spring MVC Test framework. After you have read this blog post, you can identify the key components of the Spring MVC Test framework, and get the required dependencies with Maven and Gradle.
- The Best Way to Configure the Spring MVC Test Framework, Part One helps you to select the best way to configure the Spring MVC Test framework when you are writing unit tests.
- Writing Unit Tests for "Normal" Spring MVC Controllers: Configuration describes how you can configure the system under test by using the standalone configuration when you are writing unit tests for controller methods that render data or process form submissions.
- Writing Tests for Spring MVC Controllers: Test Case 101 provides a very quick introduction to sending HTTP requests to the system under test when you are using the Spring MVC Test framework.
- Writing Unit Tests for Spring MVC Controllers: Rendering a Single Item describes how you can write unit tests for a Spring MVC controller which renders the information of a single item.
- Writing Unit Tests for Spring MVC Controllers: Rendering a List describes how you can write unit tests for a Spring MVC controller that renders a list.
- Writing Unit Tests for Spring MVC Controllers: Forms describes how you can write unit tests for a Spring MVC controller that submits a form.
- Writing Unit Tests for a Spring MVC REST API: Configuration describes how you can configure the system under test by using the standalone configuration when you are writing unit tests for a Spring MVC REST API.
- Writing Unit Tests for a Spring MVC REST API: Returning a Single Item describes how you can write unit tests for a Spring MVC API endpoint which returns the information of a single item as JSON.
- Writing Unit Tests for a Spring MVC REST API: Returning a List describes how you can write unit tests for a Spring MVC API endpoint which returns a list as JSON.
- Writing Unit Tests for a Spring MVC REST API: Writing Data describes how you can write unit tests for a Spring MVC API endpoint which inserts data into a database and returns data as JSON.
Writing Integration Tests for Spring Boot Web Applications
- The Best Way to Configure the Spring MVC Test Framework, Part Two helps you to select the best way to configure the Spring MVC Test framework when you are writing integration tests.
- The Best Tools for Writing Integration Tests for Spring Boot Web Applications highlights six testing tools which we should use when we are 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.