使用MockMVC与Junit进行单体测试
1、pom.xml追加
junit
spring-test
2、测试共通类
@ContextConfiguration(locations = { "classpath:springframework/application-context.xml",
"classpath:springframework/dispatcherservlet-servlet.xml" })
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@Transactional
abstract public class TestCommon {
private static final Logger LOG = LogManager.getLogger();
private MockMvc mockMvc;
@Autowired
private WebApplicationContext wac;
@Before
public void setUp() {
mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
}
}
3、示例
public class MemberTest extends TestCommon {
/**
* 请求:新增,请求方式:POST
*/
@Test
@Rollback(false)
public void add() throws Exception {
String uri = "/member/add";
Map<String, String> contentParams = new HashMap<>();
contentParams.put("name", "测试用姓名");
contentParams.put("sex", "男");
String jsonStr = new ObjectMapper().writeValueAsString(contentParams);
MockHttpServletResponse response = mockMvc.perform(
MockMvcRequestBuilders.post(uri).contentType(MediaType.APPLICATION_JSON_UTF8).content(jsonStr))
.andReturn().getResponse();
if (response.getStatus() != HttpServletResponse.SC_OK) {
fail("Http" + response.getStatus());
}
LOG.info(response.getContentAsString());
}
}
4、如果想测试除post以外的请求,可以调用MockMvcRequestBuilders的get, put等方法
5、类似于URL中的“?page=2”的参数,可以调用MockMvcRequestBuilders的params方法
使用MockMVC与Junit进行单体测试的更多相关文章
- Spring-test使用JUnit时,测试类autowired报错,create bean error
Spring-test使用JUnit时,测试类里面使用autowired会报错, 报create bean error...... 但是controller里面@autowired可以正常运行的. 在 ...
- Android中使用自身携带的Junit新建一个测试工程
1.新建立一个Android工程 package com.shellway.junit; public class Service { public int divide(int a,int b){ ...
- [深入JUnit] 为什么别测试private函数
[深入JUnit] 为什么别测试private函数 摘自http://www.tuicool.com/articles/iumaayJ 时间 2016-03-28 10:58:03 SegmentFa ...
- 一次单体测试的采坑--MatcherAssert.assertThat---org.hamcrest 和org.mockito
单体测试测试环境ci上报这个错, 本地没问题. org.hamcrest.Matcher.describeMismatch(Ljava/lang/Object;Lorg/hamcrest/Descri ...
- Java高级特性 第11节 JUnit 3.x和JUnit 4.x测试框架
一.软件测试 1.软件测试的概念及分类 软件测试是使用人工或者自动手段来运行或测试某个系统的过程,其目的在于检验它是否满足规定的需求或弄清预期结果与实际结果之间的差别.它是帮助识别开发完成(中间或最终 ...
- Maven聚合、Maven仓库jar包以及Spring+MyBatis+JUnit+Maven整合测试的搭建过程
一.Maven将父项目创建到父项目的内部 在父项目的pom.xml上 点右键,选择maven-->new-->maven module project 二.Maven聚合 在某个项目的p ...
- JUnit+Mockito结合测试Spring MVC Controller
[本文出自天外归云的博客园] 概要简述 利用JUnit结合Mockito,再加上spingframework自带的一些方法,就可以组合起来对Spring MVC中的Controller层进行测试. 在 ...
- springboot利用mock进行junit单元测试,测试controller
1 spring-boot-starter-test内置mockito,添加pom依赖 <dependency> <groupId>org.springframework.b ...
- spring && Cobertura && maven &&junit 单元测试以及测试覆盖率
1. 目的: junit 单元测试,Cobertura 测试覆盖率报告 项目目录结构 2. maven 配置 <project xmlns= ...
随机推荐
- url格式化函数http_build_query() 和parse_str() 函数
例子 1. http_build_query() 使用示例 <?php $data = array('foo'=>'bar', 'baz'=>'boom', 'cow'=>'m ...
- vs2015工程转化为vs2010
转换的步骤如下: (1)将工程是.sln用记事本打开后,更换以下信息如下: Microsoft Visual Studio Solution File, Format Version 11.00 ...
- Spring/Spring Boot整合Weblogic JMS实战
本文主要介绍weblogic jms的配置,包括JMS 服务器和JMS 模块(连接工厂.队列.远程 SAF 上下文.SAF 导入目的地.SAF 错误处理)的配置:并在Spring/Spring Boo ...
- SDL 实现多线程 的一些BUG
1. SDL_init() 在多个线程初始化的时候 , 在第二个线程出现SDL_init 崩溃的现象 SDL init 错误码:0XFFFFFFFF 2. SDL_init() 如果只初始化一 ...
- @app.route源码流程分析
@app.route(), 是调用了flask.app.py文件里面的Flask类的route方法,route方法所做的事情和add_url_rule类似,是用来为一个URL注册一个视图函数,但是我们 ...
- springboot 日志配置
maven依赖中添加了 spring-boot-starter-logging : <dependency> <groupId>org.springframework.boot ...
- sed 追加文件内容
追加用法总结 1.a 在匹配行后面追加 2.i 在匹配行前面追加 3.r 将文件内容追加到匹配行后面 4.w 将匹配行写入指定文件 在匹配行后面追加 a passwd文件第10行后面追加"A ...
- python urllib应用
urlopen 爬取网页 爬取网页 read() 读取内容 read() , readline() ,readlines() , fileno() , close() :这些方法的使用方式与文件对象完 ...
- 用ffserver实现rtsp服务器的实验笔记
参考:https://blog.csdn.net/hoyjam1/article/details/51281679 建议配置文件:/etc/config/ffserver.conf Port 1053 ...
- STM32 LoRaWAN探索板B-L072Z-LRWAN1中文用户手册
UM2115用户手册 支持LoRaWAN和 LPWAN协议的STM32L0探索套件 前言 B-L072Z-LRWAN1探索套件采用了 Murata公司的CMWX1ZZABZ-091 LoRa模块.该探 ...