Junit测试宁静的服务

时间:2013-07-02 15:53:02

标签: rest spring-mvc junit4 applicationcontext spring-test-mvc

我写了一个junit来离线测试我的休息服务。我的restful控制器的junit扩展了AbstractControllerTestSupport,用于创建dispatcherservlet实例。

@RunWith(SpringJUnit4ClassRunner.class)  
@ContextConfiguration(loader=MockWebContextLoader.class, locations={"/rest-servlet-   test.xml"})  
public abstract class AbstractControllerTestSupport extends TestCase {  

private static DispatcherServlet dispatcherServlet;  

....

    public static DispatcherServlet getServletInstance() {  
        if(null == dispatcherServlet) {  
            dispatcherServlet = new DispatcherServlet() {  
                protected WebApplicationContext createWebApplicationContext(WebApplicationContext parent) {  
                    return MockWebContextLoader.getInstance();
                }  
            }; 

                System.out.println("dispatcher:"+dispatcherServlet.getContextConfigLocation()+":"+dispatcherServlet.getWebApplicationContext());

            try {  
              dispatcherServlet.init(new MockServletConfig());  
            } catch (ServletException se) {  
                System.out.println("Exception"+se.getMessage());
            }  
        }  
    return dispatcherServlet;  
}

以下是我的装载机课程。

  public class MockWebContextLoader extends AbstractContextLoader {

public static final ServletContext SERVLET_CONTEXT = new MockServletContext(
        "/mHealthAPIs", new FileSystemResourceLoader());

private final static GenericWebApplicationContext webContext = new GenericWebApplicationContext();

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

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

    SERVLET_CONTEXT.setAttribute(
            WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,
            webContext);
    webContext.setServletContext(SERVLET_CONTEXT);
    createBeanDefinitionReader(webContext).loadBeanDefinitions(locations);
    AnnotationConfigUtils.registerAnnotationConfigProcessors(webContext);
    webContext.refresh();
    webContext.registerShutdownHook();
    return webContext;
}

public static WebApplicationContext getInstance() {
    return webContext;
}

protected String getResourceSuffix() {
    return "-context.xml";
}

测试运行正常,使用Spring 3.0版。但是如果我转移到spring 3.2.x它会给我以下错误“类型MockWebContextLoader必须实现继承的抽象方法SmartContextLoader.loadContext(MergedContextConfiguration)” 。这是因为在3.2.2“AbstractContextLoader”中实现了“SmartContextLoader”。

你能为我提供解决方案吗?

1 个答案:

答案 0 :(得分:1)

得到解决方案:我按如下方式更改了MockWebContextLoader类。

public class MockWebContextLoader extends AbstractContextLoader {

public static final ServletContext SERVLET_CONTEXT = new MockServletContext(
        "/mHealthAPIs", new FileSystemResourceLoader());

private final static GenericWebApplicationContext webContext = new GenericWebApplicationContext();

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

@Override
public ApplicationContext loadContext(MergedContextConfiguration arg0)
        throws Exception {

    SERVLET_CONTEXT.setAttribute(
            WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,
            webContext);
    webContext.setServletContext(SERVLET_CONTEXT);
    createBeanDefinitionReader(webContext).loadBeanDefinitions(
            arg0.getLocations());
    AnnotationConfigUtils.registerAnnotationConfigProcessors(webContext);
    webContext.refresh();
    webContext.registerShutdownHook();
    return webContext;
}

public static WebApplicationContext getInstance() {
    return webContext;
}

protected String getResourceSuffix() {
    return "-context.xml";
}

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

    return null;
}

}