I am doing a unit testing which includes parsing of data with XStream parser.I used Mockito for mocking context.But the test case fails with an error log:
java.lang.RuntimeException: Method newInstance in org.xmlpull.v1.XmlPullParserFactory not mocked. See http://g.co/androidstudio/not-mocked for details.
at org.xmlpull.v1.XmlPullParserFactory.newInstance(XmlPullParserFactory.java)
at com.thoughtworks.xstream.io.xml.XppDriver.createParser(XppDriver.java:57)
at com.thoughtworks.xstream.io.xml.AbstractXppDriver.createReader(AbstractXppDriver.java:54)
at com.thoughtworks.xstream.io.xml.AbstractXppDriver.createReader(AbstractXppDriver.java:65)
at com.thoughtworks.xstream.XStream.fromXML(XStream.java:1049)
at com.att.apis.metadata.MetadataHandler.execute(MetadataHandler.java:38)
at com.att.apis.services.ServerOperations.executePostOperation(ServerOperations.java:20)
at com.att.framework.helper.request.MetadataAPIHandler.executeAPIRequest(MetadataAPIHandler.java:58)
at com.att.framework.helper.request.MetadataAPIHandlerMokitoTest.executeAPIRequest(MetadataAPIHandlerMokitoTest.java:57)
The error is happening at the line " response = (MetadataResponse)xs.fromXML(iStream); " in the 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 the answer from Android: XmlPullParserFactory.newInstance() creating a null factory, I added
testOptions {
unitTests.returnDefaultValues = true
}
in build.gradle.
After the update when NullPointer Exception with below log occurred.
java.lang.NullPointerException
at com.thoughtworks.xstream.io.xml.XppDriver.createParser(XppDriver.java:59)
at com.thoughtworks.xstream.io.xml.AbstractXppDriver.createReader(AbstractXppDriver.java:54)
at com.thoughtworks.xstream.io.xml.AbstractXppDriver.createReader(AbstractXppDriver.java:65)
at com.thoughtworks.xstream.XStream.fromXML(XStream.java:1049)
at com.att.apis.metadata.MetadataHandler.execute(MetadataHandler.java:38)
at com.att.apis.services.ServerOperations.executePostOperation(ServerOperations.java:20)
at com.att.framework.helper.request.MetadataAPIHandler.executeAPIRequest(MetadataAPIHandler.java:58)
at com.att.framework.helper.request.MetadataAPIHandlerMokitoTest.executeAPIRequest(MetadataAPIHandlerMokitoTest.java:57)
Can anyone help me to sort out the issue
XmlPullParserFactory
comes from the Android platform and therefore can not be mocked in the unit tests as they are executed with the Java VM you have installed on your computer.
You may want to use the Robolectric framework, it allows for the unit tests to make calls to the Android platform (it mocks almost everything in the whole platform)