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

歡迎訪問 生活随笔!

生活随笔

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

javascript

java restful接口测试_详解SpringBoot restful api的单元测试

發(fā)布時間:2023/12/9 javascript 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java restful接口测试_详解SpringBoot restful api的单元测试 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

現(xiàn)在我們來利用Spring Boot來構建一個RestFul API,具體如下:

1.添加Springboot測試注解

@RunWith(SpringRunner.class)

@SpringBootTest

public class UserControllerTest {

}

2.偽造mvc環(huán)境

// 注入Spring 工廠

@Autowired

private WebApplicationContext wac;

//偽造mvc環(huán)境

private MockMvc mockMvc;

@Before

public void setup(){

mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();

}

3.引入靜態(tài)方法

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;

import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;

import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

3.編寫測試方法

@Test

public void whenXXXXSuccess() throws Exception {

//模擬發(fā)送請求

String result =

mockMvc.perform(get("/user") //發(fā)往/user的get請求,可以換成post,put,delete方法執(zhí)行相應請求

.param("username","xxx") //get請求時填寫參數(shù)的位置

.contentType(MediaType.APPLICATION_JSON_UTF8) //utf編碼

.content(content)) //post和put請求填寫參數(shù)的位置

.andExpect(status().isOk())

.andExpect(jsonPath("$.length()").value(3)) //期望的json返回結果

.andReturn().getResponse().getContentAsString(); //對返回字符串的json內容進行判斷

log.info(result);

}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

總結

以上是生活随笔為你收集整理的java restful接口测试_详解SpringBoot restful api的单元测试的全部內容,希望文章能夠幫你解決所遇到的問題。

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