日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring Boot 集成测试

發布時間:2024/9/30 javascript 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring Boot 集成测试 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、測試一般程序

1.1 測試步驟
  • 在pom.xml 中加入測試環境的依賴。
  • 在測試類上加入@RunWith(SpringRunner.class) 與@SpringBootTest 注解。
  • 1.2 簡單測試例子

    在pom.xml 中引入相關依賴

    <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope> </dependency>

    要測試的程序代碼

    @Repository public class UserDao {public void addUser(String username){if(username.equals("zhangsan")){System.out.println("===============");}else {System.out.println("---------------");}} }

    測試代碼

    @RunWith(SpringRunner.class) @SpringBootTest public class SpringbootApplicationTests {@Autowiredprivate UserDao userDao;@Testpublic void testAddUser() {userDao.addUser("zhangsan");}}

    測試結果

    二、測試 Controller

    2.1 使用TestRestTemplate 對象測試

    測試步驟

  • 在pom.xml 中加入測試環境的依賴。
  • 在測試類上加入@RunWith(SpringRunner.class)與
    @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) 注解。
  • 使用TestRestTemplate 完成測試
  • 要測試的 controller

    @RestController public class UserController{@GetMapping("/show/{id}")public String show(@PathVariable Integer id){return "show" + id; }}

    測試代碼

    /** TestRestTemplate 需要運行在 web 項目中 */ @RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) public class ControllerTest {@Autowiredprivate TestRestTemplate template;@Testpublic void testController(){// template.getForObject() 會得到 controller 返回的 json 值String content = template.getForObject("/show/100", String.class);// 使用斷言測試,使用正確的斷言Assert.assertEquals("show100", content);} }

    正確的測試結果

    當斷言不正確時,測試的結果

    2.2 使用@WebMvcTest 注解測試

    測試步驟

  • 在pom.xml 中加入測試環境的依賴。
  • 在測試類上加入@RunWith(SpringRunner.class) 與@WebMvcTest 注解。
  • 仍然使用UserController 控制器。

    測試代碼

    /** @WebMvcTest 注解需要指定測試控制器所在的類 */ @RunWith(SpringRunner.class) @WebMvcTest(UserController.class) public class ControllerTest2 {@Autowiredprivate MockMvc mvc;@Testpublic void testController() throws Exception {// 模擬請求,并期望執行成功mvc.perform(MockMvcRequestBuilders.get("/show/100").param("id", "100")).andExpect(MockMvcResultMatchers.status().isOk());// 模擬請求,并期望執行成功,以及期望其返回的值是“show100”mvc.perform(MockMvcRequestBuilders.get("/show/100").param("id", "100")).andExpect(MockMvcResultMatchers.content().string("show100"));} }

    正確的測試結果

    當期望返回的結果與請求的數據不一致時,測試的結果

    2.3@WebMvcTest 與@SpringBootTest 注解總結

    @WebMvcTest 與@SpringBootTest 注解不能在一起使用。

    還需要注意的地方是在使用@WebMvcTest 注解進行測試時,該注解不會加載在 controller 中的其他依賴。也就是說這個注解不會加載整個 Spring 容器,它只會加載在@WebMvcTest() 中配置的 bean。

    @SpringBootTest 注解會加載所有被 Spring 容器管理的 bean。

    MockMvc 對象并不常常與@WebMvcTest() 注解在一起使用,在你想使用MockMvc 對象時,又希望會加載被 Spring 容器管理的 bean,你可以這樣做。

    @RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @AutoConfigureMockMvc public class TestController3 {@Autowiredprivate MockMvc mockMvc; }

    總結

    以上是生活随笔為你收集整理的Spring Boot 集成测试的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。