SpringFramework之mvc controller的单元测试

    项目里面常常会将controller的扫描配置与其它的分开以便于管理开发,可是controller的bean是在webApplicationContext中,与web容器结合起来,怎么单元测试时该怎么作呢?java

1、手动建立webApplicationContext

    以下List-1.1所示,手动建立XmlWebApplicationContext,并指定xml路径web

    List-1.1spring

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.mock.web.MockRequestDispatcher;
import org.springframework.mock.web.MockServletContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.XmlWebApplicationContext;

@Configuration
public class WebApplicationContextConfiguration implements ApplicationContextAware {
    private ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    @Bean
    public WebApplicationContext getBean() {
        MockServletContext servletContext = new MockServletContext();
        servletContext.setMajorVersion(2);
        servletContext.setMinorVersion(4);
        servletContext.setContextPath("");
        XmlWebApplicationContext webApplicationContext = new XmlWebApplicationContext();
        webApplicationContext.setConfigLocation("classpath*:spring-mvc.xml");
        servletContext.registerNamedDispatcher("default", new MockRequestDispatcher("default"));
        webApplicationContext.setServletContext(servletContext);
        webApplicationContext.setParent(applicationContext);
        webApplicationContext.refresh();
        return webApplicationContext;
    }
}

2、建测试基类

    以下List-2.1所示spring-mvc

    List-2.1mvc

import org.junit.runner.RunWith;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath*:spring-context.xml", "classpath*:spring-db.xml"})
public class TestBase {

}

三、样例   

    以下List-3.1所示,拿到webApplicationContext,就能够作controller的单元测试了。app

    List-3.1ide

import org.junit.Assert;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.condition.PatternsRequestCondition;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

public class TestController extends TestBase {
    private static final Logger LOGGER = LoggerFactory.getLogger(TestController.class);
    @Autowired(required = true)
    private WebApplicationContext webApplicationContext;

    @Test
    public void test() {
        Assert.assertNotNull(webApplicationContext);
    }
相关文章
相关标签/搜索