Hướng dấn sử dụng Spring Boot Test controller
Hướng dấn sử dụng Spring Boot Test controller
spring-boot-test
Remain: 5
1 Answer
monkey
Enlightened
monkey
Enlightened
Bước 1. Khởi tạo project và add dependencies:
<span><span><<span>dependency</span>></span> <span><<span>groupId</span>></span>org.springframework.boot<span></<span>groupId</span>></span> <span><<span>artifactId</span>></span>spring-boot-starter-web<span></<span>artifactId</span>></span> <span><<span>version</span>></span>$</span><span>{spring.boot.version}</span><span><span></<span>version</span>></span> <span></<span>dependency</span>></span> <span><<span>dependency</span>></span> <span><<span>groupId</span>></span>org.springframework.boot<span></<span>groupId</span>></span> <span><<span>artifactId</span>></span>spring-boot-starter-test<span></<span>artifactId</span>></span> <span><<span>version</span>></span>$</span><span>{spring.boot.version}</span><span><span></<span>version</span>></span> <span><<span>scope</span>></span>test<span></<span>scope</span>></span> <span></<span>dependency</span>></span></span>
Với <spring.boot.version>2.7.0</spring.boot.version>
Bước 2. Khởi tạo các controller và service, ví du:
package com.example.spring_test.service; import org.springframework.stereotype.Service; @Service public class HelloService { public String hello(String who) { return "Hello " + who + '!'; } }
package com.example.spring_test.controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import com.example.spring_test.service.HelloService; import lombok.AllArgsConstructor; @RestController @AllArgsConstructor public class HomeController { private final HelloService helloService; @GetMapping("/hello") public String hello(@RequestParam String who) { return helloService.hello(who); } }
Bước 3. Tạo lớp test và run test, ví dụ:
package com.example.spring_test.test; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.test.web.servlet.MockMvc; import com.example.spring_test.service.HelloService; @SpringBootTest @AutoConfigureMockMvc public class HomeControllerTest { @MockBean private HelloService helloService; @Autowired private MockMvc mockMvc; @Test public void helloTest() throws Exception { // given final String who = "World"; final String hello = "Hello World!"; when(helloService.hello(who)).thenReturn(hello); // when // then mockMvc.perform(get("/hello?who=World")) .andExpect(status().isOk()) .andExpect(content().string(hello)); verify(helloService, times(1)).hello(who); } }
Done!!!
-
0