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进行单体测试的更多相关文章

  1. Spring-test使用JUnit时,测试类autowired报错,create bean error

    Spring-test使用JUnit时,测试类里面使用autowired会报错, 报create bean error...... 但是controller里面@autowired可以正常运行的. 在 ...

  2. Android中使用自身携带的Junit新建一个测试工程

    1.新建立一个Android工程 package com.shellway.junit; public class Service { public int divide(int a,int b){ ...

  3. [深入JUnit] 为什么别测试private函数

    [深入JUnit] 为什么别测试private函数 摘自http://www.tuicool.com/articles/iumaayJ 时间 2016-03-28 10:58:03 SegmentFa ...

  4. 一次单体测试的采坑--MatcherAssert.assertThat---org.hamcrest 和org.mockito

    单体测试测试环境ci上报这个错, 本地没问题. org.hamcrest.Matcher.describeMismatch(Ljava/lang/Object;Lorg/hamcrest/Descri ...

  5. Java高级特性 第11节 JUnit 3.x和JUnit 4.x测试框架

    一.软件测试 1.软件测试的概念及分类 软件测试是使用人工或者自动手段来运行或测试某个系统的过程,其目的在于检验它是否满足规定的需求或弄清预期结果与实际结果之间的差别.它是帮助识别开发完成(中间或最终 ...

  6. Maven聚合、Maven仓库jar包以及Spring+MyBatis+JUnit+Maven整合测试的搭建过程

    一.Maven将父项目创建到父项目的内部 在父项目的pom.xml上 点右键,选择maven-->new-->maven module  project 二.Maven聚合 在某个项目的p ...

  7. JUnit+Mockito结合测试Spring MVC Controller

    [本文出自天外归云的博客园] 概要简述 利用JUnit结合Mockito,再加上spingframework自带的一些方法,就可以组合起来对Spring MVC中的Controller层进行测试. 在 ...

  8. springboot利用mock进行junit单元测试,测试controller

    1  spring-boot-starter-test内置mockito,添加pom依赖 <dependency> <groupId>org.springframework.b ...

  9. spring && Cobertura && maven &&junit 单元测试以及测试覆盖率

    1. 目的:       junit 单元测试,Cobertura   测试覆盖率报告       项目目录结构          2. maven 配置     <project xmlns= ...

随机推荐

  1. 音视频入门-09-RGB&YUV互转-使用开源库

    * 音视频入门文章目录 * 介绍开源库 使用第三方开源库来简化开发,屏蔽一些底层的复杂度,节省大量编写代码的时间. libyuv: Google 开源的实现各种 YUV 与 RGB 之间相互转换.旋转 ...

  2. Effective Java 读书笔记(四):泛型

    1 不要使用原始类型 (1)术语 术语 例子 参数化类型(Parameterized type) List<String> 实际类型参数(Actual type parameter) St ...

  3. SVN_05用戶管控

    安全性设置 [1]在左侧的User上点击右键 输入上面的信息,点击OK,我们就创建一个用户了. 说明:注意到了下面图中的Groups,是的,也可以先创建组,把用户添加到各个组中,然后对组进行授权,操作 ...

  4. .net通过网络路径下载文件至本地

    获取网络文件,通过流保存文件,由于上一版存在数据丢失情况,稍微调整了以下. //网络路径文件 string pathUrl = "http://localhost:805/春风吹.mp3&q ...

  5. R_数据操作_高级_04

    数学函数: abs(x) 绝对值     sqrt(x) 平方根   ceiling(x) 放回不小于x的最小整数 floor(x) 不小于x的最大整数   trunc(x) 先0方向截取x的整数部分 ...

  6. English-培训4-How do you spend your day

  7. Apache日志轮询Cronolog安装及简单用法

    安装日志轮询工具cronolog: [root@bqh- tools]# tar xf cronolog-.tar.gz [root@bqh- tools]# cd cronolog- [root@b ...

  8. 解决 React Native:The development server returned response error code: 404

    解决方法: 打开android/app/build.gradle compile 'com.facebook.react:react-native:+' 修改为: compile ("com ...

  9. Oracle GoldenGate(ogg)安装经验大汇总,采坑总结,绝对干货!

    一下是安装ogg过程中遇到的问题和解决办法,绝对良心干货,抽空会写更详细的安装教程.更多精彩内容请点击 OGG-00685 begin time prior to oldest log in log ...

  10. SQL SERVER-端口Port

    Quick cheat sheet for port numbers used by SQL Server services or services that SQL Server may depen ...