在SpringMVC利用MockMvc进行单元测试

spring在线文档:https://docs.spring.io/spring/docs/current/javadoc-api/index.html?index-files/index-13.html

----------------------------------------------------------------------------------------------------------------

一、应用示例

package com.springapp.mvc;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.web.context.WebApplicationContext;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup;

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration("file:src/main/webapp/WEB-INF/mvc-dispatcher-servlet.xml")
public class AppTests {
    private MockMvc mockMvc;

    @SuppressWarnings("SpringJavaAutowiringInspection")
    @Autowired
    protected WebApplicationContext wac;

    @Before
    public void setup() {
        this.mockMvc = webAppContextSetup(this.wac).build();//获取mockMvc实例
    }

    @Test
    public void simple() throws Exception {
        mockMvc.perform(post("/mytest"))//执行一个RequestBuilder请求
                .andExpect(view().name("index2"))//验证返回视图是否是"index2"
                .andDo(print()).andExpect(status().isOk());//打印结果到控制台
    }


}

 

二、代码解释

@WebAppConfiguration

http://docs.spring.io/spring/docs/3.2.x/javadoc-api/org/springframework/test/context/web/WebAppConfiguration.html中有这样的解释:

@WebAppConfiguration is a class-level annotation that is used to declare that the ApplicationContext loaded for an integration test should be a WebApplicationContext.

其中文意思是:“@webappconfiguration是一级注释,用于声明一个ApplicationContext集成测试加载WebApplicationContext。

 

private MockMvc mockMvc;

https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/test/web/servlet/MockMvc.html中这样解释:

Main entry point for server-side Spring MVC test support.

其中文意思是:对于服务器端的Spring MVC测试支持主入口点。

该对象可以由方法webAppContextSetup(WebApplicationContext context).build()构造。

 

org.springframework.test.web.servlet.setup.MockMvcBuilders

先介绍些:MockMvcBuilder,MockMvcBuilder是用来构造MockMvc的构造器,其主要有两个实现:StandaloneMockMvcBuilder和DefaultMockMvcBuilder,分别对应之前的两种测试方式。对于我们来说直接使用静态工厂MockMvcBuilders创建即可(注:MockMvcBuilders在官方文档中并未找到):

MockMvcBuilders.webAppContextSetup(WebApplicationContext context):指定WebApplicationContext,将会从该上下文获取相应的控制器并得到相应的MockMvc

MockMvcBuilders.standaloneSetup(Object... controllers):通过参数指定一组控制器,这样就不需要从上下文获取了。

 

simple方法中的代码中的方法:

perform:执行一个RequestBuilder请求,会自动执行SpringMVC的流程并映射到相应的控制器执行处理;

andExpect:添加ResultMatcher验证规则,验证控制器执行完成后结果是否正确;

andDo:添加ResultHandler结果处理器,比如调试时打印结果到控制台;

 

后记:更多参加spring的官方文档

 

附:另一种测试

package com.spring.test;

import com.wql.dao.UserMapper;
import com.wql.vo.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.annotation.Resource;

/**
 * Created by Administrator on 15-12-11.
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring.xml")
public class SqlTests {
    @Resource
    private UserMapper userMapper;

    @Test
    public  void test(){
        User u =  userMapper.selectByPrimaryKey(1);
        System.out.println(u);
    }

    @Test
    public void test2(){
        User u = new User();
        u.setName("wang");
        u.setPassword("112");
        System.out.println(userMapper.checkLogin(u));
    }

}

 

 

 


 

posted @ 2015-12-05 21:59  Tom1997  阅读(9852)  评论(0编辑  收藏  举报