2013-07-02 152 views
1

我已经写了一个JUnit测试我的休息服务offline.The JUnit的我宁静的控制器扩展AbstractControllerTestSupport这是用来创建dispatcherservletinstance。JUnit来测试RESTful服务

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

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

您能否为我提供解决方法?

回答

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

}