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

3 篇文章 0 订阅
2 篇文章 0 订阅

接我前面一篇文章关于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 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
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 Graphical User Interface (GUI) development. This pattern is not only common in web development, but is also used in desktop technology like Java Swing. Sometimes called Spring Web MVC, Spring MVC is one of the most popular web frameworks today and a most sought-after skill. This book is for anyone wishing to learn to develop Java-based web applications with Spring MVC. Sample applications come as Spring Tool Suite and Eclipse projects. Table of Contents Introduction Chapter 1: The Spring Framework Chapter 2: Model 2 and the MVC Pattern Chapter 3: Introduction to Spring MVC Chapter 4: Annotation-Based Controllers Chapter 5: Data Binding and the Form Tag Library Chapter 6: Converters and Formatters Chapter 7: Validators Chapter 8: The Expression Language Chapter 9: JSTL Chapter 10: Internationalization Chapter 11: File Upload Chapter 12: File Download Chapter 13: Testing Your Application Appendix A: Tomcat Appendix B: Using Spring Tool Suite with Maven Appendix C: The Servlet API Appendix D: JavaServer Pages Appendix E: Deployment Index Table of Contents Chapter 1: The Spring Framework Chapter 2: Model 2 and the MVC Pattern Chapter 3: Introduction to Spring MVC Chapter 4: Annotation-Based Controllers Chapter 5: Data Binding and the Form Tag Library Chapter 6: Converters and Formatters Chapter 7: Validators Chapter 8: The Expression Language Chapter 9: JSTL Chapter 10: Internationalization Chapter 11: File Upload Chapter 12: File Download Chapter 13: Testing Your Application Appendix A: Tomcat Appendix B: Using Spring Tool Suite with Maven Appendix C: The Servlet API Appendix D: JavaServer Pages Appendix E: Deployment

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值