"XmlPullParserFactory not mocked" 单元测试时出现问题
"XmlPullParserFactory not mocked" issue while unit testing
我正在进行单元测试,其中包括使用 XStream parser.I 进行数据解析,使用 Mockito 进行模拟 context.But 测试用例失败并显示错误日志:
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)
错误发生在下面代码块中的“response = (MetadataResponse)xs.fromXML(iStream);”行
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);
根据Android: XmlPullParserFactory.newInstance() creating a null factory的回答,我添加了
testOptions {
unitTests.returnDefaultValues = true
}
在 build.gradle.
更新后出现以下日志的 NullPointer 异常。
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)
谁能帮我解决这个问题
XmlPullParserFactory
来自 Android 平台,因此无法在单元测试中模拟,因为它们是使用您计算机上安装的 Java VM 执行的。
您可能想要使用 Robolectric 框架,它允许单元测试调用 Android 平台(它模拟了整个平台中的几乎所有内容)
您应该稍微重构您的代码并模拟 Xstream
及其所有依赖项。例如,您可以将 Xstream
的创建外部化并将其传递给您正在测试的方法。
Xstream mockXstream = Mockito.mock(Xstream.class);
//mock all xstream method calls
void myMethodUnderTest(Xstream xs){
InputStream iStream = responseData.getInputStream();
xs.autodetectAnnotations(true);
xs.alias("helloa", A.class);
xs.alias("hellob", B.class);
xs.alias("helloc",C.class);
response = (MetadataResponse)xs.fromXML(iStream);
}
如果您认为模拟 XmlPullParser
不是一个好主意并且您宁愿针对真实的解析器编写测试,那么您可以检查您的代码是否真正正确地解析了真实的 XML 文档,这就是解决问题的方法:
只需将此 xmlpull
依赖项添加到您应用的 gradle 文件中:
testImplementation group: 'xmlpull', name: 'xmlpull', version: '1.1.3.1'
它将仅用于 运行 单元测试,不会打包到您的 APK 中
我正在进行单元测试,其中包括使用 XStream parser.I 进行数据解析,使用 Mockito 进行模拟 context.But 测试用例失败并显示错误日志:
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)
错误发生在下面代码块中的“response = (MetadataResponse)xs.fromXML(iStream);”行
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);
根据Android: XmlPullParserFactory.newInstance() creating a null factory的回答,我添加了
testOptions {
unitTests.returnDefaultValues = true
}
在 build.gradle.
更新后出现以下日志的 NullPointer 异常。
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)
谁能帮我解决这个问题
XmlPullParserFactory
来自 Android 平台,因此无法在单元测试中模拟,因为它们是使用您计算机上安装的 Java VM 执行的。
您可能想要使用 Robolectric 框架,它允许单元测试调用 Android 平台(它模拟了整个平台中的几乎所有内容)
您应该稍微重构您的代码并模拟 Xstream
及其所有依赖项。例如,您可以将 Xstream
的创建外部化并将其传递给您正在测试的方法。
Xstream mockXstream = Mockito.mock(Xstream.class);
//mock all xstream method calls
void myMethodUnderTest(Xstream xs){
InputStream iStream = responseData.getInputStream();
xs.autodetectAnnotations(true);
xs.alias("helloa", A.class);
xs.alias("hellob", B.class);
xs.alias("helloc",C.class);
response = (MetadataResponse)xs.fromXML(iStream);
}
如果您认为模拟 XmlPullParser
不是一个好主意并且您宁愿针对真实的解析器编写测试,那么您可以检查您的代码是否真正正确地解析了真实的 XML 文档,这就是解决问题的方法:
只需将此 xmlpull
依赖项添加到您应用的 gradle 文件中:
testImplementation group: 'xmlpull', name: 'xmlpull', version: '1.1.3.1'
它将仅用于 运行 单元测试,不会打包到您的 APK 中