`

【转】就是这么简单(续)!使用 RestAssuredMockMvc 测试 Spring MVC Controllers

 
阅读更多

接我前面一篇文章关于RestAssured测试Restful web service的, RestAssured还有一个功能, 使用RestAssuredMockMvc 单元测试你的Spring MVC Controllers, 这个MockMvc 是建立在Spring MockMvc基础上的, 其目的是让我们用起来更便捷。

 

Getting Ready

复制代码
<dependency>
        <groupId>com.jayway.restassured</groupId>
        <artifactId>spring-mock-mvc</artifactId>
        <version>2.4.0</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
        <scope>test</scope>
    </dependency>
    
    <!-- Optional -->
    <dependency>
        <groupId>org.hamcrest</groupId>
        <artifactId>hamcrest-core</artifactId>
        <version>1.3</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.hamcrest</groupId>
        <artifactId>hamcrest-library</artifactId>
        <version>1.3</version>
        <scope>test</scope>
    </dependency>
复制代码

 

Example

下面是我们要测试的Controller

复制代码
package com.wadeshop.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class GreetingController {

    private static final String template = "Hello, %s!";

    @RequestMapping(value = "/greeting", method = RequestMethod.GET)
    @ResponseBody 
    public Greeting greeting(@RequestParam(value="name", required=false, defaultValue="World") String name) {
        return new Greeting(String.format(template, name));
    }
}
复制代码

 

Greeting 类 如下

复制代码
public class Greeting {
    
    private final String content;
    
    public String getContent() {
        return content;
    }
 
    public Greeting(String content) {
        this.content = content;
    }
    
}
复制代码

 

##转载注明出处:http://www.cnblogs.com/wade-xu/p/4311205.html 

 

接下来就是创建Spring MVC 测试类了

复制代码
package com.wadeshop.controller;

import static com.jayway.restassured.module.mockmvc.RestAssuredMockMvc.given;
import static org.hamcrest.Matchers.equalTo;

import org.junit.Before;
import org.junit.Test;

import com.jayway.restassured.module.mockmvc.RestAssuredMockMvc;

public class GreetingControllerTest {

    @Before
    public void configured() {
        RestAssuredMockMvc.standaloneSetup(new GreetingController());
    }

    @Test
    public void test1() {
        given().
               param("name", "Johan").
         when().
               get("/greeting").
         then().
               statusCode(200).
               body("content", equalTo("Hello, Johan!"));
    }
    
    @Test
    public void test2() {
        given().
               param("name", "").
         when().
               get("/greeting").
         then().
               statusCode(200).
               body("content", equalTo("Hello, World!"));
    }

}
复制代码

 

单元测试过程无非就这些步骤:

1. 准备测试环境, 上面的例子就是使用 standalone setup 初始化MockMvc, 传入被测Controller

2. 传入参数构造请求并且调用

3. 验证结果

 

执行结果如下

 

是不是很简单?

 

这种方式其实就是纯粹的单元测试,如果想模拟真实的Spring MVC, 走Spring MVC完整流程,比如Dispatcher servlet, 类型转换,数据绑定等等, 则需要用MockMvcBuilders.webAppContextSetup(webApplicationContext).build(); 我在以后的文章中会介绍到。

 

参考

https://code.google.com/p/rest-assured/wiki/Usage#Spring_Mock_Mvc_Module

 

##转载:http://www.cnblogs.com/wade-xu/p/4311205.html 

分享到:
评论

相关推荐

    Pro Spring MVC With Web Flow

    Table of Contents Configuring a Spring Development Environment Spring Framework Fundamentals Web Application Architecture Spring MVC Architecture Implementing Controllers Implementing Controllers ...

    Spring.MVC.A.Tutorial.2nd.Edition.1771970316

    This is a tutorial on Spring MVC, a module in the Spring Framework for rapidly developing web applications. The MVC in Spring MVC stands for Model-View-Controller, a design pattern widely used in ...

    Spring.MVC.Beginner's.Guide.2nd.Edition.2016.7.pdf

    Spring MVC helps you build flexible and loosely coupled web applications. The Spring MVC Framework is architected and designed in such a way that every piece of logic and functionality is highly ...

    Spring.MVC.Cookbook.1784396419

    Configure Spring MVC to build logic-less controllers that transparently support the most advanced web techniques Build an amazing social and financial application that applies microservices patterns ...

    精通Spring.MVC

    中文名: 精通Spring MVC 原名: Pro Spring MVC With Web Flow 作者: Deinum Serneels Yates Ladd Vanfleteren图书分类: 软件 资源格式: PDF 版本: 英文版 出版社: Apress书号: 978-1-4302-4155-3发行时间: 2012年 ...

    spring MVC 最新教程

    spring MVC 最新的教程 2012年6月出品 Foreword.............................................................................................................. xvi  About the Authors.......................

    ASP.NET MVC Generic Controllers and Views

    ASP.NET MVC Generic Controllers and Views for Handling Entity Frameworks DbContext’s and objects

    Programming Microsoft ASP.NET MVC, 3rd Edition

    Dive into the functions of controllers—the heart of an MVC site Explore the structure and behavior of a view engine Process a variety of input data using a custom model binder Automate the writing of...

    MVC中使用Repository模式vs2013

    一、在MVC中开发的时候,避免在Controllers中直接访问数据,为了构建更加适应未来变化以及更加易于测试的MVC应用程序,应使用Repository模式。当你使用Repository模式时,你会创建一个独立的repository类,它包含了...

    Mvc controller 多层级目录

    Asp.net mvc 多层级 Controller Views 目录实现 的项目源文件,直接vs 打开即可运行的。

    Learning.Spring.Application.Development.1783987367

    Following this, you will explore how to implement the request handling layer using Spring annotated controllers. Other highlights include learning how to build the Java DAO implementation layer by ...

    MVC2.0入门必读教程程序源码

    本程序中包括MVC基本的使用以及MVC内置拦截器( 在ASP.NET MVC中,有三种拦截器:Action拦截器、Result拦截器和Exception拦截器。)本程序中用到了第一种和第三种。其实也没什么神秘的,就是一个普通的类而已。只不过...

    Spring 5 Design Patterns

    From here, you will be introduced to the MVC patterns and how it treats controllers as Plain Old Java Objects, and build a reactive web application using Spring MVC Pattern. Finally, we will move on ...

    Learning Spring Application Development

    Following this, you will explore how to implement the request handling layer using Spring annotated controllers. Other highlights include learning how to build the Java DAO implementation layer by ...

    ASP.NET MVC MSDN 文档 CHM

    Controllers and Action Methods in MVC Applications Views and UI Rendering in MVC Applications Models and Model Binders in MVC Applications Walkthrough: Adding ASP.NET AJAX Scripting to an MVC Project ...

    alfresco-mvc:SpringMVC @Controllers和Alfresco之间的胶合

    Alfresco和Spring MVC之间缺少的胶水 可在企业和社区上使用,并且会运行一个被广泛接受的REST框架 你应该在什么时候使用它 您需要自定义API 您想提高生产力 您编写自定义网页脚本 您将从中受益 更快的发展 Java开发...

    Manning.Spring.in.Action.4th.Edition.2014.11.epub

    7.1. Alternate Spring MVC configuration 7.1.1. Customizing DispatcherServlet configuration 7.1.2. Adding additional servlets and filters 7.1.3. Declaring DispatcherServlet in web.xml 7.2. Processing ...

    教你使用viewcontrollers以及创建展示图片

    资源名称:教你使用view controllers以及创建展示图片内容简介:通过教你制作一个上架应用 PicDecor来教你使用 view controllers以及创建展示图片本章教大家开发一个应用 PicDecor,这个应用可以允许用户从相册上传...

    Test-Drive ASP.NET MVC

    1 Getting Started with ASP.NET MVC 1.1 How ASP.NET MVC Works 1.2 Installing MVC 1.3 MVC in Five Minutes: Building Quote-O-Matic 2 Test-Driven Development 2.1 TDD Explained 2.2 Test-Driving ...

    Pro ASP.NET MVC 5 epub

    The ASP.NET MVC 5 Framework is the latest evolution of Microsoft’s ASP.NET web platform. It provides a high-productivity programming model that promotes cleaner code architecture, test-driven ...

Global site tag (gtag.js) - Google Analytics