spring-boot-starter-test is the starter for writing unit tests with Spring Boot:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
spring-boot-starter-test brings in a number of important unit testing frameworks:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
</dependency>
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
</dependency>
<dependency>
<groupId>org.skyscreamer</groupId>
<artifactId>jsonassert</artifactId>
</dependency>
Let's take a quick look at these frameworks:
- JUnit: Most popular Java unit testing framework, provides the base structure for writing unit tests.
- Spring Test (spring-test): Brings in capabilities to write unit tests to launch spring contexts. Also provides a Mock MVC framework that can be used to unit test REST APIs.
- Mockito (mockito-core): Helps in writing unit testings using mocks.
- AssertJ (assertj-core): Helps you write simple, readable assertions; for example—assertThat(numbersList).hasSize(15).contains(1, 2, 3).allMatch(x -> x < 100).
- Hamcrest (hamcrest-core): Another framework to write great assertions; for example—assertThat("XYZ", containsString("XY")).
- JSONPath (json-path): Similar to XPath for XML, you can use JSONPath for identifying and unit testing elements in your JSON responses.
- JSON Assert (JSONAssert): JSON Assert allows us to specify an expected JSON, and use it to check for specific elements in the JSON response. We will use JSON Assert a little later in this chapter.