web services - cxf request scoped bean not working in unit test (soap) -
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 spring bean request scope:
@component @scope( value=webapplicationcontext.scope_request, proxymode = scopedproxymode.target_class ) public class requestscopedclass
which fetch dynamically applicationcontext in cxf endpoint implementation:
@component @webservice(endpointinterface = "ch.xyz.paymentserviceinterface" ) public class paymentserviceimpl implements paymentserviceinterface { ... requestscopedclass rsc = appctxt.getbean( requestscopedclass.class ); rsc.dosomething();
my goal test frontend of soap service simulating client connects listener port etc., ensuring whole cxf stack interceptor chain (including custom interceptors) executed. managed setup configuration including
org.apache.cxf:cxf-rt-transports-http-jetty
dependency , starting endpoint in test setup:
string address = "http://0.0.0.0:8080/"; myendpoint = endpoint.publish( address, new paymentserviceimpl() );
running test throws beancreationexception in call rsc.dosomething():
error creating bean name 'scopedtarget.requestscopedenvironment': scope 'request' not active ...
if change proxymode 1 of 3 other possibilites, same exception thrown when fetching bean appctxt.
the test annotated
@runwith( springjunit4classrunner.class ) @contextconfiguration( classes = { ..., requestscopedclass.class } ) @webappconfiguration
if application started "gradle bootrun" on command line , soap request done chrome postman application fine , expected soap response.
what can there valid request scope when executing cxf soap server in unit test?
meanwhile found 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; } }
Comments
Post a Comment