[英]WebTestClient All negative unit test cases passed
我正在嘗試在我的 Spring 啟動應用程序中使用WebTestClient編寫單元測試。 但是當嘗試運行時,所有控制器測試用例都通過了,包括一些負面測試用例。
以下是我的 MainApplication、Controller 和 ControllerTest 代碼供您參考:
@SpringBootApplication
@EnableWebFlux
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
@RunWith( SpringRunner.class )
@WebFluxTest(controllers = {MyController.class})
@AutoConfigureWebTestClient
public class MyControllerTests {
@MockBean
MyService service;
@Autowired
private WebTestClient webTestClient;
@Test
public void test() throws Exception {
webTestClient
.get()
.uri(uriBuilder ->
uriBuilder
.path("/my-controller/test")
.build())
.exchange()
.expectBody(String.class)
.equals("Hii");
}
}
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/my-controller")
public class MyController {
private final MyService myService;
@GetMapping("/test")
public String test() throws Exception {
System.out.println(">>>>>>>>>>>>>>>>>");
return "Hello :)";
}
}
您實際上並沒有斷言響應正文是否相等。 嘗試這樣的事情:
boolean result = webTestClient
.get()
.uri(uriBuilder -> uriBuilder.path("/my-controller/test").build())
.exchange()
.expectBody(String.class)
.equals("Hii");
Assertions.assertFalse(result); // this should pass
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.