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);
    }
相關文章
相關標籤/搜索