spring 单元测试之controller 层
程序员文章站
2022-03-02 18:06:13
...
测试 Controller POST 返回 JSON
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
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 org.springframework.web.context.WebApplicationContext;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:applicationContext.xml", "classpath:spring-mvc.xml" })
@WebAppConfiguration
public class AdminTestControllerPost {
protected MockMvc mockMvc;
@Autowired
protected WebApplicationContext wac;
@Before // 这个方法在每个方法执行之前都会执行一遍
public void setup() {
mockMvc = MockMvcBuilders.webAppContextSetup(wac).build(); // 初始化MockMvc对象
}
@Test
public void testGetSequence() {
try {
MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.post("/score"))
.andExpect(MockMvcResultMatchers.status().is(200)).andDo(MockMvcResultHandlers.print()).andReturn();
int status = mvcResult.getResponse().getStatus();
System.out.println("请求状态码:" + status);
String result = mvcResult.getResponse().getContentAsString();
System.out.println("接口返回结果:" + result);
JSONObject resultObj = JSON.parseObject(result);
// 判断接口返回json中success字段是否为true
Assert.assertTrue(resultObj.getBooleanValue("success"));
} catch (Exception e) {
e.printStackTrace();
}
}
}
测试 Controller GET 返回 JSON
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:applicationContext.xml", "classpath:spring-mvc.xml" })
@WebAppConfiguration
public class AdminTestController {
protected MockMvc mockMvc;
@Autowired
protected WebApplicationContext wac;
@Before // 这个方法在每个方法执行之前都会执行一遍
public void setup() {
mockMvc = MockMvcBuilders.webAppContextSetup(wac).build(); // 初始化MockMvc对象
}
@Test
public void getAllCategoryTest() throws Exception {
String responseString = mockMvc.perform(get("/score") // 请求的url,请求的方法是get
// get("/user/showUser2") //请求的url,请求的方法是get
.contentType(MediaType.APPLICATION_FORM_URLENCODED)// 数据的格式
.param("page", "1") // 添加参数(可以添加多个)
).andExpect(status().isOk()) // 返回的状态是200
.andDo(print()) // 打印出请求和相应的内容
.andReturn().getResponse().getContentAsString(); // 将相应的数据转换为字符串
System.out.println("-----返回的json = " + responseString);
}
}
上一篇: Java 项目所用到的基础API (1)
下一篇: Ubuntu使用所用到的
推荐阅读
-
idea创建一个入门Spring Boot项目(controller层)使用Moven代码管理
-
关于Spring MVC在Controller层中注入request的坑详解
-
详解Spring Boot实战之单元测试
-
关于Spring MVC在Controller层中注入request的坑详解
-
详解Spring Boot实战之单元测试
-
idea创建一个入门Spring Boot项目(controller层)使用Moven代码管理
-
Spring MVC学习笔记之Controller查找(基于Spring4.0.3)
-
Spring Boot从Controller层进行单元测试的实现
-
【SSH进阶之路】Spring的IOC逐层深入——源码解析之IoC的根本BeanFactory(五)
-
【SSH进阶之路】Spring的IOC逐层深入——源码解析之IoC的根本BeanFactory(五)