比Dao和Service的测试稍微复杂一点。还是先写一个BasicWebTest用来总体配置:

@WebAppConfiguration
@ContextConfiguration(locations= {"classpath:spring/applicationContext.xml","classpath:spring/spring-servlet.xml"})
public class BasicWebTest extends AbstractTransactionalJUnit4SpringContextTests { @Autowired
protected WebApplicationContext wac; protected MockMvc mvc; @Before
public void setUp() {
this.mvc=MockMvcBuilders.webAppContextSetup(wac).build();
}
}
BasicWebTest 上面多了个 @WebAppConfiguration 注解,并且类中也多了一些方法、字段。直接复制吧。
测试用例如下:
package com.suyin.controller;

import java.util.List;

import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.junit.Test;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers; import com.jayway.jsonpath.JsonPath;
import com.suyin.BasicWebTest;
import com.suyin.pojo.People;
@SuppressWarnings("unchecked")
public class PeopleControllerTest extends BasicWebTest { @Test
public void queryAll() throws Exception {
mvc.perform(MockMvcRequestBuilders.get("/people/queryAll"))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.view().name("people/list"))
.andExpect(MockMvcResultMatchers.request().attribute("list", new MatcherAdapter() { @Override
public void describeMismatch(Object item, Description mismatchDescription) {
mismatchDescription.appendText("wrong list size");
} @Override
public boolean matches(Object item) {
List<People> list=(List<People>) item;
return list.size()==;
} }));
}
@Test
public void asynQueryAll() throws Exception {
MvcResult mr=mvc.perform(MockMvcRequestBuilders.get("/people/asynQueryAll").content("{\"id\":1}"))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.jsonPath("$.id").exists())
.andReturn(); MockHttpServletResponse response=mr.getResponse();
MockHttpServletRequest request=mr.getRequest();
} @Test
public void update1() throws Exception {
mvc.perform(MockMvcRequestBuilders.post("/people/update").param("id", "")
.param("name", "Jack")
.param("address", "japan")
.param("age", ""))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.view().name("people/detail")); }
@Test
public void update2() throws Exception { mvc.perform(MockMvcRequestBuilders.post("/people/update")
//.param("name", "Jack")
.param("address", "japan")
.param("age", ""))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.view().name("error"));
} @Test
public void add1()throws Exception {
mvc.perform(MockMvcRequestBuilders.post("/people/add")
.param("name", "JJJJ")
.param("address", "")
.param("age", "")
)
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.view().name("people/detail"));
}
@Test
public void add2()throws Exception {
mvc.perform(MockMvcRequestBuilders.post("/people/add")
.param("address", "")
.param("age", "")
)
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.view().name("error"));
} abstract class MatcherAdapter<T> implements Matcher<T>{ @Override
public void describeTo(Description arg0) {
// TODO Auto-generated method stub } @Override
public void _dont_implement_Matcher___instead_extend_BaseMatcher_() {
// TODO Auto-generated method stub } @Override
public abstract void describeMismatch(Object item,
Description mismatchDescription); @Override
public abstract boolean matches(Object item) ; }
}

认真说来关于Controller的Spring测试就这样了。在多就是关于MockMvc的API学习了。

spring 4.0 JUnit简单的Controller测试的更多相关文章

  1. spring 4.0 JUnit简单的Dao,Service测试

    1.AbstractTransactionalJUnit4SpringContextTests 和AbstractJUnit4SpringContextTests.我们在测试用例类要继承两种中的一个. ...

  2. 云优化的概念、Entity Framework 7.0、简单吞吐量压力测试

    云优化的概念.Entity Framework 7.0.简单吞吐量压力测试 继续上一篇<开发 ASP.NET vNext 初步总结(使用Visual Studio 2014 CTP1)>之 ...

  3. 开发 ASP.NET vNext 续篇:云优化的概念、Entity Framework 7.0、简单吞吐量压力测试

    继续上一篇<开发 ASP.NET vNext 初步总结(使用Visual Studio 2014 CTP1)>之后, 关于云优化和版本控制: 我本想做一下MAC和LINUX的self-ho ...

  4. spring boot web开发 简单的增删改查和spring boot 自带的Junit测试 案例

    创建 web项目 配置pom.xml文件   ------相当于jar包 配置application.yml -----配置文件(spring数据库连接.server服务.logging日志等) 创建 ...

  5. 021-Spring Boot 测试,Junit方式使用,mock方式,Controller测试

    一.概述 二.Junit方式使用 2.1.基本使用 eclipse在新建的类上,右键→new→Junit Test Case,修改一下Source folder,选择src/test/java,下一步 ...

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

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

  7. ssh框架整合---- spring 4.0 + struts 2.3.16 + maven ss整合超简单实例

    一 . 需求 学了这么久的ssh,一直都是别人整合好的框架去写代码,自己实际动手时才发现框架配置真是很坑爹,一不小心就踏错,真是纸上得来终觉浅! 本文将记录整合struts + spring的过程 , ...

  8. 使用spring 4.0 + maven 构建超简单的web项目

    一.需求 使用spring去管理web项目,是目前非常流行的一种思路,本文将介绍使用maven+spring 4.0.2 来构建一个简单的web项目. 二.实现 1.新建一个maven项目,如下图所示 ...

  9. Spring Boot 解决方案 - JUnit 测试

    简单的 JUnit 项目 回顾一下创建并运行简单的 JUnit 测试项目,先添加 JUnit 依赖然后编写类似如下模板的测试类,使用 IDE 的话直接用插件运行就行, 使用 Maven 的话运行命令 ...

随机推荐

  1. Never Go Away

    Hey if you ever want to leave it allif you ever want to lose control leave it all escape so far away ...

  2. django配置文件环境分离后celery的启动方式整理

    django项目中,当配置文件分离时: 启动方式1: 硬编码写死在manage.py中: os.environ.setdefault("DJANGO_SETTINGS_MODULE" ...

  3. POJ 1068 Parencodings (类似括号的处理问题)

                                                                                                    Pare ...

  4. mysql 主从,主主,主主复制时的主键冲突解决

    原理:slave 的i/o thread ,不断的去master抓取 bin_log, 写入到本地relay_log 然后sql thread不断的更新slave的数据 把主服务器所有的数据复制给从服 ...

  5. LINQ 学习路程 -- 开篇

    Enumerable: Queryable:

  6. docker仓库及数据卷

    docker help rmi, 删除本地镜像 docker run -it --name=centos centos:latest /bin/sh  --name的选项可以方便我们以后引用此imag ...

  7. M1905

    11.09    11:00------102万 11.09     14:00---103万 11.12    16:00------103万 11.19     16:00---94万 11.20 ...

  8. Hive- 大数据仓库Hive

    什么是 Hive? Hive 是由 FaceBook 开源用于解决少量数据结构化日志的数据统计.Hive是基于 Hadoop 的一个数据仓库工具,可以将结构化的数据文件映射成一张表,并提供类SQL查询 ...

  9. (转)edm注意事项

    格式编码 1.页面宽度请设定在600到800px以内,长度1024px以内. 2.HTML编码请使用utf-8. 3.HTML代码在15KB以内.(各个邮箱的收件标准不一样,如果超出15KB您的邮件很 ...

  10. node.js+express+jade系列一:session的使用

    此出只介绍内存session的配置好使用 1:打开app.js文件,添加下面红色内容,一定要注意位置在router前面 app.use(express.methodOverride()); sessi ...