欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

SpringBootControllerPost接口單元測試示例

概述

成都創(chuàng)新互聯(lián)是一家專注于網(wǎng)站設計制作、成都網(wǎng)站設計與策劃設計,綏陽網(wǎng)站建設哪家好?成都創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設十載,網(wǎng)設計領(lǐng)域的專業(yè)建站公司;建站業(yè)務涵蓋:綏陽等地區(qū)。綏陽做網(wǎng)站價格咨詢:18982081108

在日常的開發(fā)中,我們一般會定義一個service層,用于實現(xiàn)業(yè)務邏輯,并且針對service層會有與之對應的齊全的覆蓋率高的單元測試。而對于controller層,一般不怎么做單元測試,因為主要的核心業(yè)務邏輯都在service層里,controller層只是做轉(zhuǎn)發(fā),調(diào)用service層接口而已。但是還是建議使用單元測試簡單的將controller的方法跑一下,看看轉(zhuǎn)發(fā)和數(shù)據(jù)轉(zhuǎn)換的代碼是否能正常工作。

Spring Boot里對controller層進行單元測試非常簡單,只需要幾個注解和一點點輔助代碼即可搞定。

依賴的包

  <dependency>
   <groupId>org.junit.jupiter</groupId>
   <artifactId>junit-jupiter-api</artifactId>
   <scope>test</scope>
  </dependency>
  <dependency>
   <groupId>org.junit.jupiter</groupId>
   <artifactId>junit-jupiter-engine</artifactId>
   <scope>test</scope>
  </dependency>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-test</artifactId>
   <scope>test</scope>
  </dependency>
  <dependency>
   <groupId>com.alibaba</groupId>
   <artifactId>fastjson</artifactId>
  </dependency>

使用的Spring Boot 版本

2.0.4.RELEASE

代碼

@ExtendWith(SpringExtension.class)
@SpringBootTest(webEnvironment =SpringBootTest.WebEnvironment.MOCK,classes = TestApplication.class)
@AutoConfigureMockMvc
public class UserControllerTest {
 @Autowired
 private MockMvc mockMvc;
 @MockBean
 private UserService userService;
 @Test
 @DisplayName("測試controller方法")
 void test() throws Exception {
  User param = new User();
  param.setUserId(1111);
  List<Address> addressList = new ArrayList<>();
  Address address = new Address();
  address.setName("我的地址");
  addressList.add(address);
  param.setAddressList(addressList);
  MvcResult mvcResult = mockMvc.perform(
    post("/xxx/test")
      .contentType(MediaType.APPLICATION_JSON)
      .content(JSON.toJSONString(param)))
    .andReturn();
  
  System.out.println(mvcResult.getResponse().getContentAsString());
 }
}
@RequestMapping(value = "/xxx", method = RequestMethod.POST)
public Object test(@RequestBody(required = false)User user) throws Exception {
}

如果你只是想簡單的跑一下controller層,不想真正的去執(zhí)行service方法的話,需要使用@MockBean將對應的servicemock掉。

 @MockBean
 private UserService userService;

使用Spring Boot Test的時候,它需要一個ApplicationContext,我們可以在@SpringBootTest注解中使用classes屬性來指定。

@SpringBootTest(webEnvironment =SpringBootTest.WebEnvironment.MOCK,classes = TestApplication.class)

TestApplication的代碼很簡單。

@SpringBootApplication
public class TestApplication {
 public static void main(String[] args){
  SpringApplicationBuilder builder = new SpringApplicationBuilder();
  builder.environment(new StandardEnvironment());
  builder.sources(TestApplication.class);
  builder.main(TestApplication.class);
  builder.run(args);
 }
}

接下來我們只需要使用MockMvc發(fā)送post請求即可。如果controller層的post方法是帶@RequestBody注解的,可以先將入?yún)ο筠D(zhuǎn)換成JSON字符串。這里使用的是fastjson

JSON.toJSONString(param)

經(jīng)過測試,如上代碼能正常工作。

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對創(chuàng)新互聯(lián)的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接

網(wǎng)站欄目:SpringBootControllerPost接口單元測試示例
URL地址:http://chinadenli.net/article42/gjeshc.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供移動網(wǎng)站建設、關(guān)鍵詞優(yōu)化建站公司、ChatGPT網(wǎng)站導航、網(wǎng)站排名

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都seo排名網(wǎng)站優(yōu)化