1

Rest サービスをオフラインでテストするための junit を作成しました。restful コントローラーの junit は、dispatcherservletinstance の作成に使用される AbstractControllerTestSupport を拡張します。

@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 に答える