Pregunta

I have written a junit to test my rest service offline.The junit for my restful controller extends AbstractControllerTestSupport which is used to create the dispatcherservletinstance.

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

Following is my loader class.

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

the test runs fine with spring version 3.0 .However if I shift to spring 3.2.x it gives me following error "The type MockWebContextLoader must implement the inherited abstract method SmartContextLoader.loadContext(MergedContextConfiguration)" .This is because in 3.2.2 "AbstractContextLoader" implements "SmartContextLoader" .

Can you provide me with the work around?

¿Fue útil?

Solución

Got the solution:I changed the MockWebContextLoader class as follows.

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

}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top