Lesson 5: The Spring Testing Framework - part 1
Note: There is an Upgrade Notes section below for this video.
1. Goals
In this lesson, we'll have a look at basics of the Spring Testing framework.
2. Lesson Notes
The relevant module you need to import when you're starting with this lesson is: m3-spring-testing-framework-lesson-start
If you want have a look at the fully implemented lesson, as a reference, feel free to import: m3-spring-testing-framework-lesson-end
2.1. Unit Testing and Integration Testing
We’ll need to start our discussion about the fantastic testing support in Spring with a quick context about what kind of testing we’re focusing on here. And, simply put, that’s integration testing.
This is because our focus here is to explore and understand the Spring support for testing.
Unit testing is a highly useful tool, both for pure testing but also as a design tool. Of course, the Spring framework is built with unit testing and TDD in mind.
But, the actual Spring Testing Framework starts being useful as we go into integration testing, so that will be our focus.
2.2. Initial Setup
Let's add the Boot support and JUnit 5:
Let’s create the folder and package: /src/test/java and com.baeldung.ls.service.
In the next part, we'll continue with creating a test class.
Upgrade Notes
Since Boot 2.2, the Spring team has added support for JUnit 5 Jupiter into the spring-boot-starter-test by default, and therefore there is no need to include the junit-jupiter-engine dependency ourselves anymore.
Together with this, they also included a junit-vintage-engine library, which supported running JUnit 4 tests using the JUnit 5 Jupiter platform. If we don't need to run JUnit 4 tests in our project, we can exclude the Vintage Engine transitive dependency:
Later on, since version 2.4, Spring finally removed the Vintage Engine library from the spring-boot-starter-test dependencies. So, for newer versions, we don't have to explicitly exclude the library ourselves, but instead, we do have to include it if we want to support tests using JUnit 4 features.