java - jersey2 Unit testing,HttpServletRequest is null -
ask help?
jersey bug connection: [1]: https://java.net/jira/browse/jersey-2412
the servlet request, response , context not injected class when using test provider (tested jetty , grizzly2). using packages annotation pull application.
have other way?
public class vmresourcetest extends basetest { @test public void testcreatevm() { string bodydata = loadclasspathdata(class_path+file.separator+"tools"+file.separator+"createvm.json"); response response = target("/tool/cloud/vrm/fm/ghca_vms").queryparam("platform_id", "a22a4b0c3aec49f5916ea8cc01f56e9a") .request().header("x-auth-ghca-user-id", "x-auth-ghca-user-id") .post(entity.entity(bodydata, mediatype.application_json)); assertequals("200", response.getstatus()); } }
public class basetest extends jerseytest{ public string class_path = "classpath:"; public webtarget target; public client client; @override protected application configure() { enable(testproperties.log_traffic); enable(testproperties.dump_entity); resourceconfig rc = new resourceconfig().packages("com.ghca.easyview.server.api.resource"); rc.register(springlifecyclelistener.class); rc.register(requestcontextlistener.class); rc.property("contextconfiglocation", "classpath:spring/spring-config.xml"); return rc; } public string loadclasspathdata(string classfilepath){ file schemacontextfile = null; string result = ""; try { schemacontextfile = readschemafile(classfilepath); bufferedreader br = new bufferedreader(new filereader(schemacontextfile)); string s = null; while((s = br.readline())!=null){ result = result + "\n" +s; } br.close(); } catch (ioexception e) { e.printstacktrace(); } return result; } }
@component @path("tool/cloud/vrm") public class vmresource extends baseresource{ @autowired private vmservice vmservice; @context public httpservletrequest request; @context public httpservletresponse response; @post @path("{platform_type}/ghca_vms") @consumes({mediatype.application_json,mediatype.application_xml}) @produces({mediatype.application_json,mediatype.application_xml}) public response createvm(@pathparam("platform_type") string platformtype, @queryparam("platform_id") string platformid) {}
request , response null.
you need configure jerseytest
servlet environment. in jerseytest
, should have like
@override protected testcontainerfactory gettestcontainerfactory() { return new grizzlywebtestcontainerfactory(); } @override protected deploymentcontext configuredeployment() { resourceconfig config = new resourceconfig(sessionresource.class); return servletdeploymentcontext.forservlet( new servletcontainer(config)).build(); }
if @ servletdeploymentcontext.forservlet
, returns servletdeploymentcontext.builder
. if @ javadoc, see familiar looking methods, initparam(...,...)
, addlistener
, etc. building web.xml programmatically. keep chaining methods, build.
with above configuration, no longer need override configure
method in jerseytest
. add resourceconfig
seen above.
see other test examples here
also see related:
Comments
Post a Comment