java - Using PowerMock with embedded Tomcat -


i'm trying mock static method called embedded tomcat instance. here test class:

@runwith(powermockrunner.class) @preparefortest(externalapi.class) @powermockignore({"javax.management.*"}) public class testexternalapi {      @before     public void setup() {         tomcat tomcat = new tomcat();         tomcat.setport(8080);         tomcat.enablenaming();         tomcat.addwebapp("/app", new file("src/test/webapp").getabsolutepath());         tomcat.start();     }      @test     public void testapi() {         mockstatic(externalapi.class);         when(externalapi.getdata()).thenreturn(new data());          //call tomcat triggering call externalapi.getdata()     } } 

with configuration following exception:

java.lang.nosuchmethodexception: org.apache.catalina.deploy.webxml addservlet     @ org.apache.tomcat.util.introspectionutils.callmethod1(introspectionutils.java:849)     @ org.apache.tomcat.util.digester.setnextrule.end(setnextrule.java:201)     @ org.apache.tomcat.util.digester.digester.endelement(digester.java:1060)     @ com.sun.org.apache.xerces.internal.parsers.abstractsaxparser.endelement(abstractsaxparser.java:609)     @ com.sun.org.apache.xerces.internal.impl.xmldocumentfragmentscannerimpl.scanendelement(xmldocumentfragmentscannerimpl.java:1783) ... 

now thought maybe need tell powermock ignore org.apache.* packages, exception:

java.lang.classcastexception: com.sun.org.apache.xerces.internal.jaxp.saxparserfactoryimpl cannot cast javax.xml.parsers.saxparserfactory     @ javax.xml.parsers.factoryfinder.newinstance(factoryfinder.java:190)     ... 54 more 

i have tried ignoring org.* packages, works, externalapi.getdata() method not mocked @ all. suppose happens because nothing tomcat instance mocked in case, since ignoring org.apache.* packages.

why exceptions , how should configure powermock?

edit: i'm using tomcat 7. tests run fine using embedded tomcat instance if powermock not used.

i think should set required stuff when using tomcat in embedded mode protocol , life cycle listeners if , servlet classes , context parameters...

sample code worked me...

    tomcat = new tomcat();      // trigger loading of catalina.properties     catalinaproperties.getproperty("foo");      file appbase = new file(gettemporarydirectory(), "webapps");     if (!appbase.exists() && !appbase.mkdir()) {         assert.fail("unable create appbase test");     }      string protocol = getprotocol();      connector connector = new connector(protocol);     // listen on localhost     connector.setattribute("address",     inetaddress.getbyname("localhost").gethostaddress());     // use random free port     connector.setport(0);     // set reduce timeouts during async tests     connector.setattribute("connectiontimeout", "3000");     tomcat.getservice().addconnector(connector);     tomcat.setconnector(connector);      // add aprlifecyclelistener if using apr connector     if (protocol.contains("apr")) {         standardserver server = (standardserver) tomcat.getserver();         aprlifecyclelistener listener = new aprlifecyclelistener();         listener.setsslrandomseed("/dev/urandom");         server.addlifecyclelistener(listener);         connector.setattribute("pollerthreadcount", integer.valueof(1));     }      file catalinabase = gettemporarydirectory();     tomcat.setbasedir(catalinabase.getabsolutepath());     tomcat.gethost().setappbase(appbase.getabsolutepath());      system.setproperty("org.apache.catalina.startup.exit_on_init_failure", "true");      context ctx = tomcat.addcontext(getcontext(), appbase.getabsolutepath());     ctx.setparentclassloader(embeddedtomcattestbase.class.getclassloader());     //set execution independent of current thread context classloader (compatibility exec:java mojo)      wrapper w = tomcat.addservlet(ctx, "servletname", servletclass.class.getname());     w.setloadonstartup(1);     w.addmapping("/servleturi/*");     w.addmapping("/servleturi");     w.addinitparameter("statuspage", "/diagnostic/status.jsp");       wrapper w1 = tomcat.addservlet(ctx, "statusjsp", jspservlet.class.getname());     w1.addmapping("/diagnostic/status.jsp");      wrapper w3 = tomcat.addservlet(ctx, "errorjsp", jspservlet.class.getname());     w3.addmapping("/diagnostic/error.jsp");       ctx.addparameter("embeddedmode", "true");      ctx.addapplicationlistener(servletcontextlistnereclass.class.getname());      contextconfig contextconfig = new contextconfig();     ctx.addlifecyclelistener(contextconfig);      tomcat.initwebappdefaults(ctx);     tomcat.start();      // initialize engine / invoke java methods of interest here.. 

Comments

Popular posts from this blog

java - Jasper subreport showing only one entry from the JSON data source when embedded in the Title band -

serialization - Convert Any type in scala to Array[Byte] and back -

SonarQube Plugin for Jenkins does not find SonarQube Scanner executable -