一起测试CXF和Jersey会导致Spring冲突吗?

发布于 2024-09-12 17:56:17 字数 2500 浏览 1 评论 0原文

我有一个应用程序,它使用 CXF 作为 SOAP 客户端,并使用 Jersey 来提供 REST 服务,并由 Spring 管理 Jersey 类。这在 Tomcat 中运行良好;然而,当尝试使用 JerseyTest 进行测试时,我遇到了 Spring 冲突; JerseyTest 似乎没有正确关闭 Spring 上下文。

Jersey 的测试初始化​​如下所示:

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 测试的一部分,但我不知道如何执行此操作。任何指示将不胜感激。

I have an app that uses CXF as a SOAP client and Jersey to present REST services, with the Jersey classes managed by Spring. This works fine running in Tomcat; however when attempting to test with JerseyTest I get Spring conflicts; it appears JerseyTest doesn't shut-down the Spring context correctly.

The test initialisation for Jersey looks like:

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

The CXF tests (which talk to our upstream provider's test servers) looks like:

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

When Maven runs the tests the Jersey class is run first; this results in the following error when running the CXF tests:

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)

Unfortunately there doesn't seem to be any way to force the shutdown of the Spring application-context at the end of the Jersey tests and forcing per-test forks hasn't helped. It looks like I need to reset the Spring application-context as part of setting-up the CXF test, but I can't see how I would do this. Any pointers would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文