

How to test our REST services using Spring Boot?
source link: https://marco.dev/2017/01/29/how-to-test-our-rest-services-in-spring-boot/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Spring Boot offers us easy to use features to test our REST service. Our goal is to check that our REST Controller answers correctly to an HTTP request.
Here we are not interested to test the services layer or the database. For this reason we have to mock everything is around the controller.

Code sample
You can find the working code for the REST controller here:
Mock the request
Spring boot gives us the possibility to simulate the http request without any browser using the annotation @WebMvcTest . This annotation configure @MockMvc that simulates the request for the tests.
Mock the service
As we see in the picture we don’t need to test the service, maybe it is retrieving some data from the database. We can use the annotations @MockBean or @SpyBean.
@MockBean
creates a mock of the entire object.
@SpyBean
allows to mock only some methods of an object.
To the code
Import the required classes:
@RunWith(SpringRunner.class)
@WebMvcTest(DavisController.class)
public class DavisControllerTest {
In our case we have specified the controller to test in the annotation.
@SpyBean
private DavisService davisService;
@Autowired
private MockMvc mvc;
We use @SpyBean
because we are interested in mocking only the method that access the database.
@Test
public void resultListTest() throws Exception {
String expectedResult = "[{\"year\":2015,\"winner\":\"Marco Team\"}]";
given(this.davisService.getResultList()).willReturn(mockedDavisData());
this.mvc.perform(get("/result_list"))
.andExpect(status().isOk())
.andExpect(content().json(expectedResult));
}
In our test we define an expectedResult
, part of the JSON returned as answer.
To mock the ‘database method’ getResultList()
we use the features of mockito that is imported with our spring boot pom:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>
spring-boot-starter-test
</artifactId>
</dependency>
The method call this.mvc.perform(get("/result_list"))
is a static method that mock the GET request. It builds an HttpServletRequest behind the scenes and wait for the Http answer of the controller.
At this point we can test the result of the controller with the JSON string defined as expected result.
andExpect
is part of the interface ResultActions.
Author
Marco Molteni
Marco Molteni Blog
Recommend
-
12
How it works The annotation @WebMvcTest configure only the components that usually interest the web development.
-
27
java-version.com: What's new in Java 16? 15? Keep up to date! Angular, Spring Boot, REST, Security and OAuth2 - Part 1 - How to protect...
-
26
Develop Spring Boot REST API in AWS - PART 1/4 (CodeBuild + ECR)
-
10
-
13
Spring Boot Groovy PostgreSQL CRUD REST API by Didin J., updated on Mar 13, 2022 The comprehensive step by step tutorial on building CRUD REST API using Spring Boot, Groovy, and PostgreSQL In...
-
9
Spring Boot, Security, PostgreSQL, and Keycloak REST API OAuth2 by Didin J., updated on Mar 07, 2022
-
10
How to create REST API using Java Spring BootSkip to content
-
9
OAuth klient pre REST API Githubu cez Spring Boot ~/robonovotny
-
9
In this Spring Boot REST API Best Practices - Part-2, I will explain some of the best practices we should follow while implementing Create and Update API endpoints.This article is a continuation of
-
8
In this Spring Boot REST API Best Practices Series, I will explain some of the best practices we should follow while implementing REST APIs. Also, I will explain some of the common mistakes developers do and how to avoid them....
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK