Spring Boot Integration test sử dụng có cả mockup lẫn service thật theo các bước như sau:
1. Khởi tạo project theo các bước này: https://stackask.com/question/huong-dan-su-dung-spring-boot-test-controller/
2. Giả sử chúng ta có GoodbyeController
thế này:
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.GoodByeService; import com.example.spring_test.service.HelloService; import lombok.AllArgsConstructor; @RestController @AllArgsConstructor public class GoodbyeController { private final HelloService helloService; private final GoodByeService goodByeService; @GetMapping("/hello-and-goodbye") public String hello(@RequestParam String who) { return helloService.hello(who) + " and " + goodByeService.goodBye(who); } }
Và lớp GoodByeService
thế này:
package com.example.spring_test.service; import org.springframework.stereotype.Service; @Service public class GoodByeService { public String goodBye(String who) { return "Goodbye " + who + '!'; } }
Tuy nhiên chúng vì một lý do nào đó (có thê do lớp GoodbyeService
cần gọi sang service thứ 3) nên chúng ta cần mockup nó, vậy hãy sang bước tiếp theo.
Giả sử bạn có lớp startup thế này:
3. Chúng ta cần tạo ra lớp
BaseIntegrationTest
thế này:4. Cuối cùng chúng ta sẽ tạo ra lớp
GoodbyeControllerTest
thế này:Done!!!
Mình thấy ide báo lỗi.
Mình sửa thêm như này thì được.