本文介绍了cxf请求范围内的bean在单元测试中不起作用(肥皂)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

CXF soap应用程序,使用以下版本:

CXF soap application, using following versions:

springBootVersion = 1.2.3.RELEASE
springVersion     = '4.1.6.RELEASE'
cxfVersion        = '3.1.0'
junitVersion      = '4.12'

我有一个具有请求范围的spring bean:

I have a spring bean with a request scope:

@Component
@Scope( value=WebApplicationContext.SCOPE_REQUEST, proxyMode = ScopedProxyMode.TARGET_CLASS )
public class RequestScopedClass

我从中动态获取我的CXF端点实现中的ApplicationContext:

which I fetch dynamically from ApplicationContext in my CXF endpoint implementation:

@Component
@WebService(endpointInterface = "ch.xyz.PaymentServiceInterface" )
public class PaymentServiceImpl implements PaymentServiceInterface
{
    ...
    RequestScopedClass rsc = appCtxt.getBean( RequestScopedClass.class );
    rsc.doSomething();

我的目标是通过模拟连接到侦听器端口的客户端来测试soap服务的前端等等,确保执行了整个带有拦截器链的cxf堆栈(包括我的自定义拦截器)。我设法通过包含

My goal is to test the frontend of the soap service by simulating a client which connects to the listener port etc., ensuring that the whole cxf stack with the interceptor chain (including my custom interceptors) is executed. I managed to setup this configuration by including the

org.apache.cxf:cxf-rt-transports-http-jetty

依赖性并在测试设置中启动端点:

dependency and starting the endpoint in test setup:

String address = "http://0.0.0.0:8080/";
myEndpoint = Endpoint.publish( address, new PaymentServiceImpl() );

运行测试会在调用rsc.doSomething()时引发BeanCreationException:

Running the test throws BeanCreationException in call to rsc.doSomething():

Error creating bean with name 'scopedTarget.requestScopedEnvironment': Scope 'request' is not active ...

如果我将proxyMode更改为其他三个可能性之一,则从appCtxt中获取bean时已经抛出了相同的异常。

If I change the proxyMode to one of the three other possibilites, the same exception is already thrown when fetching the bean from the appCtxt.

测试用

@RunWith( SpringJUnit4ClassRunner.class )
@ContextConfiguration( classes = {
                               ...,
                               RequestScopedClass.class
                             }
)
@WebAppConfiguration

如果应用程序是通过命令行上的 gradle bootrun启动的,并且chrome邮递员应用程序完成了soap请求,那么一切都很好,并且我得到了预期的soap响应。

If the application is started by "gradle bootrun" on command line and the soap request is done by the chrome postman application everything is fine and I get the expected soap response.

我该怎么办在单元测试中执行cxf soap服务器时,是否存在有效的请求范围?

What can I do that there is a valid request scope when executing the cxf soap server in a unit test?

推荐答案

同时我找到了解决方案:

Meanwhile I found the solution:

...
import org.springframework.context.ConfigurableApplicationContext;

@Autowired
private ConfigurableApplicationContext myCtxt;


@Before
public void setUp() throws Throwable
{
    myCtxt.getBeanFactory().registerScope( "session", new CustomScope4Test() );
    myCtxt.getBeanFactory().registerScope( "request", new CustomScope4Test() );
}

public class CustomScope4Test implements Scope
{

    private final Map<String, Object> beanMap = new HashMap<String, Object>();

    /**
     * @see org.springframework.beans.factory.config.Scope#get(java.lang.String, org.springframework.beans.factory.ObjectFactory)
     */
    public Object get( String name, ObjectFactory<?> factory )
    {
        Object bean = beanMap.get( name );
        if ( null == bean )
        {
            bean = factory.getObject();
            beanMap.put( name, bean );
        }
        return bean;
    }

    /**
     * @see org.springframework.beans.factory.config.Scope#getConversationId()
     */
    public String getConversationId()
    {
        // not needed
        return null;
    }

    /**
     * @see org.springframework.beans.factory.config.Scope#registerDestructionCallback(java.lang.String, java.lang.Runnable)
     */
    public void registerDestructionCallback( String arg0, Runnable arg1 )
    {
        // not needed
    }

    /**
     * @see org.springframework.beans.factory.config.Scope#remove(java.lang.String)
     */
    public Object remove( String obj )
    {
        return beanMap.remove( obj );
    }

    /**
     * @see org.springframework.beans.factory.config.Scope#resolveContextualObject(java.lang.String)
     */
    public Object resolveContextualObject( String arg0 )
    {
        // not needed
        return null;
    }
}

这篇关于cxf请求范围内的bean在单元测试中不起作用(肥皂)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 17:36