SpringMVCでのJunitの使用

3864 ワード

springmvcの開発では、ユニットテストを行い、サーバを起動したくない場合があります.springはAbstractContextLoaderを提供し、xxx-servletを手動でロードすることができます.xmlファイル:
     1.まず模倣クラスを構築します
import javax.servlet.ServletContext;

import org.springframework.beans.factory.support.BeanDefinitionReader;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigUtils;
import org.springframework.core.io.FileSystemResourceLoader;
import org.springframework.mock.web.MockServletContext;
import org.springframework.test.context.support.AbstractContextLoader;
import org.springframework.web.context.support.StaticWebApplicationContext;

public class MockServletContextWebContextLoader extends AbstractContextLoader {

	private static final String SERVLET_CONTEXT_WEB_ROOT = "/webapp";

    @Override
    public final ConfigurableApplicationContext loadContext(String... locations) throws Exception {

        /*
         * Use a type which implements ConfigurableWebApplicationContext!
         */
        StaticWebApplicationContext context = new StaticWebApplicationContext();
        prepareContext(context);
        customizeBeanFactory(context.getDefaultListableBeanFactory());
        createBeanDefinitionReader(context).loadBeanDefinitions(locations);
        AnnotationConfigUtils.registerAnnotationConfigProcessors(context);
        customizeContext(context);
        context.refresh();
        context.registerShutdownHook();
        return context;
    }

    protected void prepareContext(StaticWebApplicationContext context) {

        /*
         * Incorporate mock servlet context!
         */
        ServletContext servletContext = new MockServletContext(SERVLET_CONTEXT_WEB_ROOT,
                new FileSystemResourceLoader());
        servletContext.setAttribute(StaticWebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,
                context);
        context.setServletContext(servletContext);

    }

    protected void customizeBeanFactory(DefaultListableBeanFactory beanFactory) {
    }

    protected BeanDefinitionReader createBeanDefinitionReader(StaticWebApplicationContext context) {
        return new XmlBeanDefinitionReader(context);
    }

    protected void customizeContext(StaticWebApplicationContext context) {
    }

    @Override
    protected String getResourceSuffix() {
        return "-servlet.xml";
    }

}

 2.テストクラスの作成
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.AbstractJUnit4SpringContextTests;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;


/**
*     Spring      ,  AbstractJUnit4SpringContextTests 
*          
*/
@ContextConfiguration(inheritLocations = true, loader = MockServletContextWebContextLoader.class,locations = { "file:src/main/webapp/WEB-INF/xxx-servlet.xml"})
@RunWith(SpringJUnit4ClassRunner.class)
public class TestBase extends AbstractJUnit4SpringContextTests {
	
	//     
	public @Autowired XxxClass test;
	@Test
	public void test() {
        // TODO   
        // test.xxx();
        }


			
}