使用MockMvc可以做到controller层的测试。如果直接得到controller,然后调用指定的方法是不会通过拦截器等外层组件的,使用mockMvc可以。
package com.lomi.mockMvc;
import cn.hutool.json.JSONUtil;
import com.lomi.controller.TestController;
import com.lomi.entity.Goods;
import com.lomi.service.GoodsService;
import lombok.extern.slf4j.Slf4j;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import java.sql.SQLOutput;
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@Slf4j
public class MockMvcTest {
// 自己写的controller
@Autowired
private TestController testController;
private MockMvc mockMvc;
@Before
public void setup() {
//初始化mockMvc
mockMvc = MockMvcBuilders.standaloneSetup(testController).build();
}
/**
* get
*
* @throws Exception
*/
@Test
public void t1() throws Exception {
MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders
.get("/test/test3")
)
//期望的响应状态200
.andExpect(MockMvcResultMatchers.status().isOk())
//打印请求过程
.andDo(MockMvcResultHandlers.print())
//返回结果
.andReturn();
log.warn("响应参数:{}",mvcResult.getResponse().getContentAsString());
}
/**
* get带参数
*
* @throws Exception
*/
@Test
public void t2() throws Exception {
MvcResult mvcResult = mockMvc.perform(
MockMvcRequestBuilders.get("/test/test2").param("name","张三")
).andExpect(MockMvcResultMatchers.status().isOk())
.andDo(MockMvcResultHandlers.print())
.andReturn();
log.warn("响应参数:{}",mvcResult.getResponse().getContentAsString());
}
/**
* post,json请求带参数,返回json
*
* @throws Exception
*/
@Test
public void t3() throws Exception {
Goods goods = Goods.randomGoods();
MvcResult mvcResult = mockMvc.perform(
MockMvcRequestBuilders.post("/test/test3").contentType(MediaType.APPLICATION_JSON_VALUE).content(JSONUtil.toJsonStr( goods )).header(HttpHeaders.ACCEPT,"application/json")
).andExpect(MockMvcResultMatchers.status().isOk())
.andDo(MockMvcResultHandlers.print())
.andReturn();
log.warn("响应参数:{}",mvcResult.getResponse().getContentAsString());
}
/**
* post,json请求带参数,返回xml
*
* @throws Exception
*/
@Test
public void t4() throws Exception {
Goods goods = Goods.randomGoods();
MvcResult mvcResult = mockMvc.perform(
MockMvcRequestBuilders.post("/test/test3").contentType(MediaType.APPLICATION_JSON_VALUE).content(JSONUtil.toJsonStr( goods ))
).andExpect(MockMvcResultMatchers.status().isOk())
.andDo(MockMvcResultHandlers.print())
.andReturn();
log.warn("响应参数:{}",mvcResult.getResponse().getContentAsString());
}
}
标签:mockMvc,import,springframework,Controller,测试,org,test,MockMvc,throws
From: https://www.cnblogs.com/cxygg/p/17484319.html