我有一个应用程序,它使用CXF作为SOAP客户端,Jersey使用Jersey来呈现REST服务,其中Jersey类由Spring管理。这在Tomcat中运行良好;但是当我试图用JerseyTest测试时,我会遇到Spring冲突;看来JerseyTest没有正确关闭Spring上下文。
泽西岛的测试初始化如下:
public MailProviderTest()
throws Exception
{
super(new WebAppDescriptor.Builder("net.haltcondition.service.rest")
.contextPath("")
.contextParam("contextConfigLocation", "classpath:applicationContext.xml")
.servletClass(SpringServlet.class)
.contextListenerClass(ContextLoaderListener.class)
.build());
}
CXF测试(与我们的上游提供商的测试服务器通信)如下所示:
@Before
public void setup()
{
// We need to do this the hard way to set the test endpoint rather than the default
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setServiceClass(Soap.class);
factory.setAddress("https://webservice.test.provider.com/Service.asmx");
soap = (Soap) factory.create();
Map<String,Object> outProps= new HashMap<String,Object>();
outProps.put(WSHandlerConstants.ACTION, WSHandlerConstants.USERNAME_TOKEN);
outProps.put(WSHandlerConstants.USER, "TESTUSER");
outProps.put(WSHandlerConstants.PASSWORD_TYPE, WSConstants.PW_TEXT);
outProps.put(WSHandlerConstants.PW_CALLBACK_REF, new WSAuthHandler("XXXX"));
Client cxfClient = ClientProxy.getClient(soap);
Endpoint cxfEndpoint = cxfClient.getEndpoint();
cxfEndpoint.getOutInterceptors().add(new WSS4JOutInterceptor(outProps));
}
当Maven运行测试时,Jersey类首先运行;这会导致运行CXF测试时出现以下错误:
Caused by: java.lang.IllegalStateException: BeanFactory not initialized or already closed - call 'refresh' before accessing beans via the ApplicationContext
at org.springframework.context.support.AbstractRefreshableApplicationContext.getBeanFactory(AbstractRefreshableApplicationContext.java:153)
at org.springframework.context.support.AbstractApplicationContext.containsBean(AbstractApplicationContext.java:892)
at org.apache.cxf.configuration.spring.ConfigurerImpl.configureBean(ConfigurerImpl.java:143)
at org.apache.cxf.configuration.spring.ConfigurerImpl.configureBean(ConfigurerImpl.java:113)
at org.apache.cxf.transport.http.AbstractHTTPTransportFactory.configure(AbstractHTTPTransportFactory.java:228)
不幸的是,似乎没有任何方法可以在Jersey测试结束时强制关闭Spring应用程序上下文,并强迫每个测试分支没有帮助。看起来我需要重置Spring应用程序上下文作为设置CXF测试的一部分,但我看不出我会怎么做。任何指针都会受到赞赏。