Testing Jersey-Spring Integration with JerseyTest, Maven and TestNG -


i want test jersey resources jersey test-framework.

i followed descriptions provided here

to create simple example. example hosted git repository on http://github.com/rmetzler/jersey-test .

$ mvn jetty:run works expected keep getting nullpointerexceptions when running $ mvn clean test.

java.lang.nullpointerexception @ com.sun.jersey.spi.container.containerresponse.mapexception(containerresponse.java:429) @ com.sun.jersey.server.impl.application.webapplicationimpl._handlerequest(webapplicationimpl.java:1295) @ com.sun.jersey.server.impl.application.webapplicationimpl.handlerequest(webapplicationimpl.java:1239) @ com.sun.jersey.test.framework.impl.container.inmemory.testresourceclienthandler.handle(testresourceclienthandler.java:119) @ com.sun.jersey.api.client.client.handle(client.java:616) @ com.sun.jersey.api.client.webresource.handle(webresource.java:559) @ com.sun.jersey.api.client.webresource.get(webresource.java:182) @ example.jersey.spring.myresourcetest.testmyresource(myresourcetest.java:30) ... 

i bet made small mistake i'm unable find. show source developer unfortunately work alone @ home. maybe of me?

update

i created eclipse project running $ mvn eclipse:eclipse . when run test junit test in eclipse green. when running testng test fails. guess has how test executed testng.

i did same thing except using guice, not spring. implementation (sorry, no time clean up, you'll have extract interesting parts yourself). note used delegate jerseytest class can inherit code base test class. have map junit pre- , post-methods testng ones.

hope helps bit.

 package mypackage;  import java.io.ioexception; import java.io.stringreader; import java.net.uri; import java.net.url; import java.util.map; import java.util.set;  import javax.ws.rs.core.mediatype; import javax.ws.rs.core.uribuilder; import javax.xml.xmlconstants; import javax.xml.parsers.documentbuilder; import javax.xml.parsers.documentbuilderfactory; import javax.xml.parsers.parserconfigurationexception; import javax.xml.transform.dom.domsource; import javax.xml.validation.schema; import javax.xml.validation.schemafactory; import javax.xml.validation.validator;  import org.apache.commons.lang.unhandledexception; import org.apache.xerces.dom.dominputimpl; import org.codehaus.jackson.jsonnode; import org.codehaus.jackson.map.objectmapper; import org.mozilla.javascript.context; import org.mozilla.javascript.functionobject; import org.mozilla.javascript.scriptable; import org.mozilla.javascript.scriptableobject; import org.slf4j.logger; import org.slf4j.loggerfactory; import org.testng.assert; import org.testng.annotations.afterclass; import org.testng.annotations.beforeclass; import org.w3c.dom.document; import org.w3c.dom.ls.lsinput; import org.w3c.dom.ls.lsresourceresolver; import org.xml.sax.inputsource; import org.xml.sax.saxexception;  import static org.fest.assertions.assertions.assertthat; import static org.fest.reflect.core.reflection.staticmethod;  import com.sun.jersey.api.client.client; import com.sun.jersey.api.client.uniforminterfaceexception; import com.sun.jersey.api.client.webresource; import com.sun.jersey.api.client.config.clientconfig; import com.sun.jersey.api.client.config.defaultclientconfig; import com.sun.jersey.api.core.packagesresourceconfig; import com.sun.jersey.api.core.resourceconfig; import com.sun.jersey.guice.spi.container.servlet.guicecontainer; import com.sun.jersey.spi.container.webapplication; import com.sun.jersey.spi.container.webapplicationfactory; import com.sun.jersey.test.framework.appdescriptor; import com.sun.jersey.test.framework.jerseytest; import com.sun.jersey.test.framework.lowlevelappdescriptor; import com.sun.jersey.test.framework.impl.container.inmemory.testresourceclienthandler; import com.sun.jersey.test.framework.spi.container.testcontainer; import com.sun.jersey.test.framework.spi.container.testcontainerexception; import com.sun.jersey.test.framework.spi.container.testcontainerfactory; import com.sun.jersey.test.framework.spi.container.inmemory.inmemorytestcontainerfactory;  import mypackage.staticconfig; import mypackage.mediatypes;  public abstract class jerseyintegrationtestbase extends transactionalintegrationtestbase {      private static final logger log = loggerfactory.getlogger( jerseyintegrationtestbase.class );      private static final class guiceinmemorytestcontainerfactory extends inmemorytestcontainerfactory {          @override         public testcontainer create( final uri baseuri, final appdescriptor ad ) {             return new guiceinmemorytestcontainer( baseuri, (lowlevelappdescriptor) ad );         }          /**          * kopie der klasse im inmemory-testcontainer von jersey, leicht guice-injection angepasst. class defines methods          * starting/stopping in-memory test container, , running tests on container.          */         private static class guiceinmemorytestcontainer implements testcontainer {              final uri baseuri;              final resourceconfig resourceconfig;              final webapplication webapp;              /**              * creates instance of {@link guiceinmemorytestcontainer}              *               * @param base              *            uri of application              * @param              *            instance of {@link lowlevelappdescriptor}              */             private guiceinmemorytestcontainer( final uri baseuri, final lowlevelappdescriptor ad ) {                 this.baseuri = uribuilder.fromuri( baseuri ).build();                  log.info( "creating low level inmemory test container configured @ base uri " + this.baseuri );                  resourceconfig = ad.getresourceconfig();                 // falls man mal in tests die requests und responses sehen möchte:                 // this.resourceconfig.getproperties().put( resourceconfig.property_container_request_filters,                 // loggingfilter.class.getname() );                 // this.resourceconfig.getproperties().put( resourceconfig.property_container_response_filters,                 // loggingfilter.class.getname() );                 resourceconfig.getproperties().putall( staticconfig.getjerseyparams() );                 webapp = webapplicationfactory.createwebapplication();             }              @override             public client getclient() {                 clientconfig clientconfig = null;                 final set providersingletons = resourceconfig.getprovidersingletons();                  if ( providersingletons.size() > 0 ) {                     clientconfig = new defaultclientconfig();                     ( final object providersingleton : providersingletons ) {                         clientconfig.getsingletons().add( providersingleton );                     }                 }                  final client client = clientconfig == null                     ? new client( new testresourceclienthandler( baseuri, webapp ) )                     : new client( new testresourceclienthandler( baseuri, webapp ), clientconfig );                  return client;             }              @override             public uri getbaseuri() {                 return baseuri;             }              @override             public void start() {                 if ( !webapp.isinitiated() ) {                     log.info( "starting low level inmemory test container" );                     webapp.initiate( resourceconfig, new guicecontainer( null ).new servletguicecomponentproviderfactory(                             resourceconfig, integrationtestbase.getinjector() ) );                 }             }              @override             public void stop() {                 if ( webapp.isinitiated() ) {                     log.info( "stopping low level inmemory test container" );                     webapp.destroy();                 }             }          }     }      private final jerseytest _jerseytest;      public jerseyintegrationtestbase() {          // packagesresourceconfig.getpackages macht genau das, wir wollen, ist aber private, daher         // auf die harte tour...         // formatter: off         final string[] packages =                 staticmethod( "getpackages" ).withreturntype( string[].class ).withparametertypes( map.class )                     .in( packagesresourceconfig.class ).invoke( staticconfig.getjerseyparams() );         // formatter: on         _jerseytest = new jerseytest( new lowlevelappdescriptor.builder( packages ).build() ) {             @override             protected testcontainerfactory gettestcontainerfactory() throws testcontainerexception {                 return new guiceinmemorytestcontainerfactory();             }         };     }      /**      * @return      * @see jerseytest#client().      */     protected client client() {         return _jerseytest.client();     }      @beforeclass( alwaysrun = true )     public void setup() throws exception {         _jerseytest.setup();     }      @afterclass( alwaysrun = true )     public void teardown() throws exception {         _jerseytest.teardown();     } } 

Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

html - Instapaper-like algorithm -

c# - How to execute a particular part of code asynchronously in a class -