java - "XmlPullParserFactory not mocked" issue while unit testing -
i doing unit testing includes parsing of data xstream parser.i used mockito mocking context.but test case fails error log:
java.lang.runtimeexception: method newinstance in org.xmlpull.v1.xmlpullparserfactory not mocked. see http://g.co/androidstudio/not-mocked details. @ org.xmlpull.v1.xmlpullparserfactory.newinstance(xmlpullparserfactory.java) @ com.thoughtworks.xstream.io.xml.xppdriver.createparser(xppdriver.java:57) @ com.thoughtworks.xstream.io.xml.abstractxppdriver.createreader(abstractxppdriver.java:54) @ com.thoughtworks.xstream.io.xml.abstractxppdriver.createreader(abstractxppdriver.java:65) @ com.thoughtworks.xstream.xstream.fromxml(xstream.java:1049) @ com.att.apis.metadata.metadatahandler.execute(metadatahandler.java:38) @ com.att.apis.services.serveroperations.executepostoperation(serveroperations.java:20) @ com.att.framework.helper.request.metadataapihandler.executeapirequest(metadataapihandler.java:58) @ com.att.framework.helper.request.metadataapihandlermokitotest.executeapirequest(metadataapihandlermokitotest.java:57)
the error happening @ line " response = (metadataresponse)xs.fromxml(istream); " in below code block
inputstream istream = responsedata.getinputstream(); xstream xs = new xstream(); xs.autodetectannotations(true); xs.alias("helloa", a.class); xs.alias("hellob", b.class); xs.alias("helloc",c.class); response = (metadataresponse)xs.fromxml(istream);
as per answer android: xmlpullparserfactory.newinstance() creating null factory, added
testoptions { unittests.returndefaultvalues = true }
in build.gradle.
after update when nullpointer exception below log occurred.
java.lang.nullpointerexception @ com.thoughtworks.xstream.io.xml.xppdriver.createparser(xppdriver.java:59) @ com.thoughtworks.xstream.io.xml.abstractxppdriver.createreader(abstractxppdriver.java:54) @ com.thoughtworks.xstream.io.xml.abstractxppdriver.createreader(abstractxppdriver.java:65) @ com.thoughtworks.xstream.xstream.fromxml(xstream.java:1049) @ com.att.apis.metadata.metadatahandler.execute(metadatahandler.java:38) @ com.att.apis.services.serveroperations.executepostoperation(serveroperations.java:20) @ com.att.framework.helper.request.metadataapihandler.executeapirequest(metadataapihandler.java:58) @ com.att.framework.helper.request.metadataapihandlermokitotest.executeapirequest(metadataapihandlermokitotest.java:57)
can me sort out issue
xmlpullparserfactory
comes android platform , therefore can not mocked in unit tests executed java vm have installed on computer.
you may want use robolectric framework, allows unit tests make calls android platform (it mocks in whole platform)
Comments
Post a Comment