java - Problems running JerseyTest when dealing with HttpServletResponse -
here sample resource class:
@path("/resource") public class someresource { @get @produces({mediatype.application_xml}) public string somemethod(@queryparam("param1") string param1, ..., @context httpservletrequest request) { string remoteuser = request.getremoteaddr(); // business logic here. return response; } }
and jerseytest resource:
public class testsomeresource extends jerseytest { @override protected application configure() { enable(testproperties.log_traffic); return new resourceconfig(someresource.class); } @test public void testxmlresponse() { string response = target("resource") .queryparam("param1", param1) // more parameters here. .request() .accept(mediatype.application_xml) .get(string.class); // assertions on response. } }
i able run jersey tests other resources except ones using @context httpservletrequest
input parameter. gives internalservererrorexception: http 500 internal server error.
following stacktrace:
javax.ws.rs.internalservererrorexception: http 500 internal server error @ org.glassfish.jersey.client.jerseyinvocation.converttoexception(jerseyinvocation.java:904) @ org.glassfish.jersey.client.jerseyinvocation.translate(jerseyinvocation.java:749) @ org.glassfish.jersey.client.jerseyinvocation.access$500(jerseyinvocation.java:88) @ org.glassfish.jersey.client.jerseyinvocation$2.call(jerseyinvocation.java:650) @ org.glassfish.jersey.internal.errors.process(errors.java:315) @ org.glassfish.jersey.internal.errors.process(errors.java:297) @ org.glassfish.jersey.internal.errors.process(errors.java:228) @ org.glassfish.jersey.process.internal.requestscope.runinscope(requestscope.java:421) @ org.glassfish.jersey.client.jerseyinvocation.invoke(jerseyinvocation.java:646) @ org.glassfish.jersey.client.jerseyinvocation$builder.method(jerseyinvocation.java:375) @ org.glassfish.jersey.client.jerseyinvocation$builder.get(jerseyinvocation.java:275) @ com.mysample.testsomeresource.testxmlresponse(testsomeresource.java:15) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:39) @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:25) @ java.lang.reflect.method.invoke(method.java:597) @ org.junit.runners.model.frameworkmethod$1.runreflectivecall(frameworkmethod.java:47) @ org.junit.internal.runners.model.reflectivecallable.run(reflectivecallable.java:12) @ org.junit.runners.model.frameworkmethod.invokeexplosively(frameworkmethod.java:44) @ org.junit.internal.runners.statements.invokemethod.evaluate(invokemethod.java:17) @ org.junit.internal.runners.statements.runbefores.evaluate(runbefores.java:26) @ org.junit.internal.runners.statements.runafters.evaluate(runafters.java:27) @ org.junit.runners.parentrunner.runleaf(parentrunner.java:271) @ org.junit.runners.blockjunit4classrunner.runchild(blockjunit4classrunner.java:70) @ org.junit.runners.blockjunit4classrunner.runchild(blockjunit4classrunner.java:50) @ org.junit.runners.parentrunner$3.run(parentrunner.java:238) @ org.junit.runners.parentrunner$1.schedule(parentrunner.java:63) @ org.junit.runners.parentrunner.runchildren(parentrunner.java:236) @ org.junit.runners.parentrunner.access$000(parentrunner.java:53) @ org.junit.runners.parentrunner$2.evaluate(parentrunner.java:229) @ org.junit.runners.parentrunner.run(parentrunner.java:309) @ org.eclipse.jdt.internal.junit4.runner.junit4testreference.run(junit4testreference.java:50) @ org.eclipse.jdt.internal.junit.runner.testexecution.run(testexecution.java:38) @ org.eclipse.jdt.internal.junit.runner.remotetestrunner.runtests(remotetestrunner.java:467) @ org.eclipse.jdt.internal.junit.runner.remotetestrunner.runtests(remotetestrunner.java:683) @ org.eclipse.jdt.internal.junit.runner.remotetestrunner.run(remotetestrunner.java:390) @ org.eclipse.jdt.internal.junit.runner.remotetestrunner.main(remotetestrunner.java:197)
you exception related fact httpservletrequest
null
.
jersey documentation says:
3.6. use of @context
previous sections have introduced use of @context. chapter 5 of jax-rs specification presents standard jax-rs java types may used @context.
when deploying jax-rs application using servlet servletconfig, servletcontext, httpservletrequest , httpservletresponse available using @context.
i'm guessing use jersey-test-framework-provider-grizzly2
doesn't support it.
if want have access httpservletresponse
remove dependency , add:
<dependency> <groupid>org.glassfish.jersey.test-framework</groupid> <artifactid>jersey-test-framework-core</artifactid> <version>2.1</version> </dependency> <dependency> <groupid>javax.servlet</groupid> <artifactid>javax.servlet-api</artifactid> <version>3.0.1</version> </dependency> <dependency> <groupid>org.glassfish.jersey.containers</groupid> <artifactid>jersey-container-grizzly2-servlet</artifactid> <version>2.1</version> </dependency>
now want tell jerseytest start right test server, have override method protected testcontainerfactory gettestcontainerfactory()
. please sure replace <your-java-package>
actual name of package.
@override protected testcontainerfactory gettestcontainerfactory() throws testcontainerexception { return new testcontainerfactory() { @override public testcontainer create(final uri baseuri, final applicationhandler application) throws illegalargumentexception { return new testcontainer() { private httpserver server; @override public clientconfig getclientconfig() { return null; } @override public uri getbaseuri() { return baseuri; } @override public void start() { try { this.server = grizzlywebcontainerfactory.create( baseuri, collections.singletonmap("jersey.config.server.provider.packages", "<your-java-package>") ); } catch (processingexception e) { throw new testcontainerexception(e); } catch (ioexception e) { throw new testcontainerexception(e); } } @override public void stop() { this.server.stop(); } }; } }; }
you can check org.glassfish.jersey.test.grizzly.grizzlytestcontainerfactory
better implementation of factory.
Comments
Post a Comment