cxf request scoped bean not working in unit test (

2019-08-10 12:04发布

问题:

CXF soap application, using following versions:

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

I have a spring bean with a request scope:

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

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

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

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

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

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.

The test is annotated by

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

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.

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

回答1:

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