1

我编写了一个 junit 来离线测试我的休息服务。我的休息控制器的 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" 。

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

4

1 回答 1

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;
}

}
于 2013-07-04T12:47:37.293 回答