Testing is a critical part of any software development process. It helps ensure that your code is working as expected and helps catch bugs early on.
JUnit is a popular unit testing framework for Java. Spring Boot provides first-class support for JUnit tests. In this article, we'll take a look at how to set up and write Spring Boot tests with JUnit.
To start, you'll need to add the following dependencies to your pom.xml
:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
The spring-boot-starter-test
dependency includes a number of useful libraries for writing tests, including JUnit, Hamcrest, and Mockito.
Spring Boot tests can be written in either JUnit 4 or JUnit 5. In this section, we'll take a look at an example of each.
Here's a simple example of a JUnit 4 test:
@RunWith(SpringRunner.class)
@SpringBootTest
public class MyTest {
@Test
public void test() {
// test code goes here
}
}
The @RunWith
annotation is used to specify which testing framework to use. The @SpringBootTest
annotation tells Spring Boot to load the application context for the test.
The @Test
annotation is used to mark a method as a test method.
JUnit 5 tests look similar to JUnit 4 tests, but with a few key differences:
@SpringBootTest
public class MyTest {
@Test
public void test() {
// test code goes here
}
}
First, the @RunWith
annotation is no longer required. Second, the @Test
annotation now belongs to the org.junit.jupiter.api
package.
JUnit 5 also introduces a new set of assertions, which can be found in the org.junit.jupiter.api.Assertions
class. For example, the assertEquals
assertion can be used like this:
assertEquals(expected, actual);
Spring Boot provides a number of useful annotations for writing tests. In this section, we'll take a look at some of the most commonly used annotations.
The @WebMvcTest
annotation is used to test Spring MVC controllers. It can be used like this:
@WebMvcTest(MyController.class)
public class MyControllerTest {
@Autowired
private MockMvc mvc;
@Test
public void test() {
// test code goes here
}
}
The @WebMvcTest
annotation will only load the components required for testing Spring MVC. This can be useful for reducing the time it takes to load the application context for tests.
The @Autowired
annotation is used to inject the MockMvc
bean. The MockMvc
bean can be used to send HTTP requests to the controller and verify the response.
The @DataJpaTest
annotation is used to test Spring Data JPA repositories. It can be used like this:
@DataJpaTest
public class MyRepositoryTest {
@Autowired
private MyRepository repository;
@Test
public void test() {
// test code goes here
}
}
The @DataJpaTest
annotation will only load the components required for testing Spring Data JPA. This can be useful for reducing the time it takes to load the application context for tests.
The @Autowired
annotation is used to inject the MyRepository
bean. This bean can be used to access the data in the database.
The @RestClientTest
annotation is used to test Spring RestTemplate clients. It can be used like this:
@RestClientTest(MyClient.class)
public class MyClientTest {
@Autowired
private MockRestTemplate template;
@Test
public void test() {
// test code goes here
}
}
The @RestClientTest
annotation will only load the components required for testing Spring RestTemplate. This can be useful for reducing the time it takes to load the application context for tests.
The @Autowired
annotation is used to inject the MockRestTemplate
bean. The MockRestTemplate
bean can be used to send HTTP requests to the client and verify the response.
In this article, we've looked at how to set up and write tests with Spring Boot and JUnit. We've also seen some of the most commonly used annotations for writing tests.