模拟 WLResponse 为 HttpResponse 抛出 ClassNotFoundException
Posted
技术标签:
【中文标题】模拟 WLResponse 为 HttpResponse 抛出 ClassNotFoundException【英文标题】:Mocking WLResponse throws ClassNotFoundException for HttpResponse 【发布时间】:2015-10-15 01:39:39 【问题描述】:我正在为一个使用 MFP 7.1 的 android 项目编写单元测试,并且我正在尝试测试一些将 WLResponse
对象作为输入的功能。我想模拟这个对象,因为我唯一需要的就是通过调用其getResponseJSON
方法返回的JSONObject
。
JSONObject jsonObject = Mockito.mock(JSONObject.class);
Mockito.when(jsonObject.toString()).thenReturn(person.toString());
WLResponse wlResponse = Mockito.mock(WLResponse.class);
Mockito.when(wlResponse.getResponseJSON()).thenReturn(jsonObject);
问题是当我调用Mockito.mock(WLResponse.class)
时,我在运行时抛出以下异常:
java.lang.NoClassDefFoundError: org/apache/http/HttpResponse
at java.lang.Class.getDeclaredConstructors0(Native Method)
at java.lang.Class.privateGetDeclaredConstructors(Class.java:2671)
at java.lang.Class.getDeclaredConstructors(Class.java:2020)
at org.mockito.internal.creation.jmock.ClassImposterizer.setConstructorsAccessible(ClassImposterizer.java:75)
at org.mockito.internal.creation.jmock.ClassImposterizer.imposterise(ClassImposterizer.java:70)
at org.mockito.internal.creation.jmock.ClassImposterizer.imposterise(ClassImposterizer.java:56)
at org.mockito.internal.creation.CglibMockMaker.createMock(CglibMockMaker.java:23)
at org.mockito.internal.util.MockUtil.createMock(MockUtil.java:26)
at org.mockito.internal.MockitoCore.mock(MockitoCore.java:51)
at org.mockito.Mockito.mock(Mockito.java:1243)
at org.mockito.Mockito.mock(Mockito.java:1120)
at com.ibm.mil.cafejava.GsonConverterTest.before(GsonConverterTest.java:26)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:78)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:212)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Caused by: java.lang.ClassNotFoundException: org.apache.http.HttpResponse
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 38 more
我正在使用通过 jcenter 提供的 MFP 7.1 SDK,具体来说:
compile 'com.ibm.mobile.foundation:ibmmobilefirstplatformfoundation:7.1.0'
据我推断,WLResponse
有一个构造函数,它接受一个org.apache.http.HttpResponse
对象作为参数。但是查看 MFP 使用的依赖项,它依赖于 android-async-http 库,而后者又依赖于 httpclient-android 库。这是第二个库,它具有第一个使用的HttpResponse
类,但它所在的包是cz/msebera/android/httpclient/
而不是org/apache/http/
。也许这可能是错误的根源,因为 WLResponse
构造函数期待 org.apache.http.HttpResponse
而不是,阻止 Mockito 能够模拟它。
【问题讨论】:
【参考方案1】:这可能是由于Android 6.0 removes support for the Apache HTTP client.假设您的 compileSdkVersion
是 23,请尝试将以下内容添加到您的 build.gradle
:
android
compileSdkVersion 23
useLibrary 'org.apache.http.legacy'
【讨论】:
以上是关于模拟 WLResponse 为 HttpResponse 抛出 ClassNotFoundException的主要内容,如果未能解决你的问题,请参考以下文章