Controller层的单元测试
程序员文章站
2022-02-12 21:22:43
...
一 代码位置
https://gitee.com/cakin24/code/tree/master/07/UnitTestDemo
二 代码
package com.example.demo.controller;
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.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.context.SpringBootTest;
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 org.springframework.web.context.WebApplicationContext;
import static org.junit.Assert.*;
@SpringBootTest
@RunWith(SpringRunner.class)
public class HelloControllerTest {
//启用web上下文
@Autowired
private WebApplicationContext webApplicationContext;
private MockMvc mockMvc;
@Before
public void setUp() throws Exception{
//使用上下文构建mockMvc
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
}
@Test
public void hello() throws Exception {
// 得到MvcResult自定义验证
// 执行请求
MvcResult mvcResult= mockMvc.perform(MockMvcRequestBuilders.get("/hello")
//.post("/hello") 发送post请求
.contentType(MediaType.APPLICATION_JSON_UTF8)
//传入参数
.param("name","longzhonghua")
// .accept(MediaType.TEXT_HTML_VALUE))
//接收的类型
.accept(MediaType.APPLICATION_JSON_UTF8))
//等同于Assert.assertEquals(200,status);
//判断接收到的状态是否是200
.andExpect(MockMvcResultMatchers.status().isOk())
//等同于 Assert.assertEquals("hello longzhonghua",content);
.andExpect(MockMvcResultMatchers.content().string("hello longzhonghua"))
.andDo(MockMvcResultHandlers.print())
//返回MvcResult
.andReturn();
//得到返回代码
int status=mvcResult.getResponse().getStatus();
//得到返回结果
String content=mvcResult.getResponse().getContentAsString();
//断言,判断返回代码是否正确
Assert.assertEquals(200,status);
//断言,判断返回的值是否正确
Assert.assertEquals("hello longzhonghua",content);
}
}
三 测试
1 测试通过
2 控制台打印如下
MockHttpServletRequest:
HTTP Method = GET
Request URI = /hello
Parameters = {name=[longzhonghua]}
Headers = [Content-Type:"application/json;charset=UTF-8", Accept:"application/json;charset=UTF-8"]
Body = null
Session Attrs = {}
Handler:
Type = com.example.demo.controller.HelloController
Method = public java.lang.String com.example.demo.controller.HelloController.hello(java.lang.String)
Async:
Async started = false
Async result = null
Resolved Exception:
Type = null
ModelAndView:
View name = null
View = null
Model = null
FlashMap:
Attributes = null
MockHttpServletResponse:
Status = 200
Error message = null
Headers = [Content-Type:"application/json;charset=UTF-8", Content-Length:"18"]
Content type = application/json;charset=UTF-8
Body = hello longzhonghua
Forwarded URL = null
Redirected URL = null
Cookies = []
上一篇: js 动态插入表格设置样式 js设置样式
下一篇: 获取项目中所用到的jar的路径
推荐阅读
-
EasyUI实现第二层弹出框的方法_jquery
-
怎么能知道XML对象中有几个一层的元素 simplexml_load_file()
-
JS实现的添加弹出层并完成锁屏操作示例
-
jQuery实现可拖动的浮动层完整代码
-
vue解决弹出蒙层滑动穿透问题的方法
-
mysql_connect localhost和127.0.0.1的区别(网络层阐述)_PHP教程
-
如何能知道XML对象中有几个一层的元素 simplexml_load_file()
-
Symfony2实现在controller中获取url的方法_php实例
-
关于页面嵌入swf覆盖div层的问题的解决方法_javascript技巧
-
代码非常简洁且兼容多浏览器的拖动层实现代码