Junit使用
1、安装junit插件
2、创建测试方法
鼠标移动到类大括号里
创建测试类,点击小绿箭头执行即可。
使用MokcMvc参数
package com.dp.controller;
import com.dp.DpEquipmentApplication;
import com.google.gson.JsonObject;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
@SpringBootTest(classes = DpEquipmentApplication.class)
@AutoConfigureMockMvc
class HdAcceptanceControllerTest {
@Autowired
private MockMvc mvc;
@Test
void selectHdAcceptance() throws Exception {
//post请求
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("name","测试");
mvc.perform(post("http://192.168.14.4:8802/hdAcceptance/selectHdAcceptance").accept(MediaType.parseMediaType("application/json;charset=UTF-8")).contentType(MediaType.APPLICATION_JSON).content(jsonObject.toString())).andDo(print()).andExpect(MockMvcResultMatchers.status().isOk());
//get请求
// mvc.perform(MockMvcRequestBuilders.get("http://192.168.14.4:8802/hdAcceptance/getText").contentType(MediaType.APPLICATION_JSON).accept(MediaType.parseMediaType("application/json;charset=UTF-8")).param("name", "测试")).andDo(print()).andExpect(MockMvcResultMatchers.status().isOk());
}
}