从Spring3.2开始 Spring包含了一种mockSpringMVC并针对controller执行http请求的机制

如(该代码选自spring实战4):

	public void shouldShowRecentSpittles() throws Exception{
List<Spittle> expectedSpittles = createSpittleList(20);
SpittleRespository mockRepository =
mock(SpittleRespository.class);
when(mockRepository.findSpittles(Long.MAX_VALUE, 20))
.thenReturn(expectedSpittles); SpittleController controller =
new SpittleController(mockRepository);
MockMvc mockMvc = standaloneSetup(controller)
.setSingleView(
new InternalResourceView("/WEB-INF/views/spittles.jsp"))
.build(); mockMvc.perform(get("/spittles"))
.andExpect(view().name("spittles"))
.andExpect(model().attributeExists("spittleList"))
.andExpect(model().attribute("spittleList",
CoreMatchers.hasItems(expectedSpittles.toArray())));
}

  

public interface SpittleRespository {

	List<Spittle> findSpittles(long max, int count);
public Spittle findOne(long id); }

  mock 以及when thenreturn 的用法在我的理解看来就是 mock方法实现了一个接口对象 when thenreturn方法对该接口对象的方法写死传入参数及返回值   之后再利用mockMvc模拟http方法请求controller 并且验证view model等属性

mock SpringMVC 测试控制器方法的更多相关文章

  1. mock中测试private方法,不是mock

    Method method = PowerMockito.method(CategoryController.class, "getCategory",List.class);// ...

  2. 无所不能的PowerMock,mock私有方法,静态方法,测试私有方法,final类

    1.为什么要用mock 我的一本书的解释: (1)创建所需的DB数据可能需要很长时间,如:调用别的接口,模拟很多数据 (2)调用第三方API接口,测试很慢, (3)编写满足所有外部依赖的测试可能很复杂 ...

  3. SpringMVC 测试 mockMVC

    SpringMVC测试框架 基于RESTful风格的SpringMVC的测试,我们可以测试完整的Spring MVC流程,即从URL请求到控制器处理,再到视图渲染都可以测试. 一 MockMvcBui ...

  4. SpringMvc测试框架详解----服务端测试

    随着RESTful Web Service的流行,测试对外的Service是否满足期望也变的必要的.从Spring 3.2开始Spring了Spring Web测试框架,如果版本低于3.2,请使用sp ...

  5. ASP.NET Core 中文文档 第四章 MVC(4.5)测试控制器逻辑

    原文: Testing Controller Logic 作者: Steve Smith 翻译: 姚阿勇(Dr.Yao) 校对: 高嵩(Jack) ASP.NET MVC 应用程序的控制器应当小巧并专 ...

  6. SpringMVC中控制器接收JSP页面表单的参数接收方式详解及细节注意(400错误)

    控制器方法中参数的接收 (1)以前的参数接收: String  param = req.getParameter(name): (2)SpringMVC简化这个操作,只需要给控制器方法添加参数即可 a ...

  7. SpringMVC的controller方法中注解方式传List参数使用@RequestBody

    在SpringMVC控制器方法中使用注解方式传List类型的参数时,要使用@RequestBody注解而不是@RequestParam注解: //创建文件夹 @RequestMapping(value ...

  8. 关于laravel5.5控制器方法参数依赖注入原理深度解析及问题修复

    在laravel5.5中,可以根据控制器方法的参数类型,自动注入一个实例化对象,极大提升了编程的效率,但是相比较与Java的SpringMVC框架,功能还是有所欠缺,使用起来还是不太方便,主要体现在方 ...

  9. ASP.NET CORE 中用单元测试测试控制器

    之前用ASP.NET CORE做的项目 加了一个新功能,数据库加了个字段balabala.... 更新到服务器上,新功能测试正常,然后就没管了..... 今天客户说网站有BUG,某个页面打开后出错了, ...

随机推荐

  1. fasttext和cnn的比较,使用keras imdb看效果——cnn要慢10倍。

    fasttext: '''This example demonstrates the use of fasttext for text classification Based on Joulin e ...

  2. Linux_服务器_09_新虚拟机下linux网络配置

    一.设置VMnet8 控制面板—>网络和Internet—>网络共享中心—>更改网络适配器,即可进入网络连接 找到VMnet8,右键—>属性—>Internet协议版本4 ...

  3. Hibernate(1)

    一.什么是hibernate 1. hibernate是开源的轻量级框架,应用在javaee三层结构中 dao层框架,使用orm思想对数据库进行crud操作 2 .在dao层里面做对数据库crud操作 ...

  4. 二叉排序树的应用(java)

    package com.tree.find; public class TestSearchBST { private static class BiNode{ int data; BiNode lc ...

  5. nyoj-115-城市平乱(dijkstra算法)

     题目链接 /* Name:nyoj-115-城市平乱 Copyright: Author: Date: 2018/4/25 17:28:06 Description: dijkstra模板题 枚举从 ...

  6. New Concept English three (51)

    22 76 Predicting the future is notoriously difficult. Who could have imagined, in the mid 1970s, for ...

  7. mysql触发器与hash索引

    url查询哈希值的维护 触发器 2.1 创建表 pseudohash. 2.2 创建触发器,当对表进行插入和更新时,触发 触发器 delimiter |create trigger pseudohas ...

  8. CodeForces - 827A:String Reconstruction (基础并查集)

    Ivan had string s consisting of small English letters. However, his friend Julia decided to make fun ...

  9. Python之高级库socketserver

    socket并不能多并发,只能支持一个用户,socketserver 简化了编写网络服务程序的任务,socketserver是socket的在封装.socketserver在python2中为Sock ...

  10. keepalived之 ipvsadm-1.26-4(lvs)+ keepalived-1.2.24 安装

    一.安装 LVS 前提:已经提前配置好本地 Yum 源 配置过程可参考> http://blog.csdn.net/zhang123456456/article/details/56690945 ...