groovy - TJWSEmbeddedJaxrsServer and ContainerRequestFilter -
i writing integration test based on spock test our rest api using jax-rs client api , tjwsembeddedjaxrsserver
. work smooth far write test integrating server request filter.
since @provider
classes implementing containerrequestfilter
interface not discovered , called @ runtime wanted ask how have register filter class manually?
can give me hint on one?
@contextconfiguration("classpath:applicationcontextrestintegrationtest.xml") @activeprofiles(['local', 'compliance']) @testexecutionlisteners([dependencyinjectiontestexecutionlistener.class, dirtiescontexttestexecutionlistener.class]) class restspec extends specification implements jaxrsinterceptorregistrylistener { @autowired applicationcontext applicationcontext @autowired ksdapischemarequestfilter filter @override void registryupdated(jaxrsinterceptorregistry registry) { println "xxxx" } void "test rest api"() { given: embeddedjaxrsserver server = new tjwsembeddedjaxrsserver() server.port = 9999 server.start() def dispatcher = server.getdeployment().getdispatcher() //dispatcher.registry.getresourceinvoker() dispatcher.providerfactory.clientrequestfilters.listeners.add(this) // register instance of methodresourceinvoker dispatcher.providerfactory.clientrequestfilters.registersingleton(filter) dispatcher.registry.addresourcefactory(new springresourcefactory('compliancetestresource', applicationcontext, compliancetestresource.class)) def url = 'http://localhost:9999//v1/compliancetest' def client = clientbuilder.newbuilder().property('connection.timeout', 1).register(jacksonjsonprovider.class).build() when: response response = client.target(url).request(application_json_value).options() then: response.status == 200 cleanup: server.stop() } }
edit 1 contentrequestfilter
spring bean , therefore have register instance of spring bean.
clientrequestfilters.registersingleton(..)
registers filter since underlaying methodresourceinvoker
not registered listener
in registry
, registry uses invoker @ runtime, registersingleton
not propagated.
is there way instance of methodresourceinvoker
in spock test? need add listener list of clientrequestfactory
(see listing above)
just use dispatcher.providerfactory.register(requestfilterinstance)
, clientrequestfilters used client side not server.
providerfactory.register()
can used both classes , configured instances, e.g., springbeans.
Comments
Post a Comment