Mocking WLResponse会为HttpResponse

时间:2015-10-15 01:39:40

标签: android unit-testing ibm-mobilefirst

我正在为使用MFP 7.1的Android项目编写单元测试,我尝试测试一些以WLResponse对象作为输入的功能。我想模仿这个对象,因为我唯一需要的就是通过调用JSONObject方法返回getResponseJSON

    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 个答案:

答案 0 :(得分:3)

这可能是因为Android 6.0 removes support for the Apache HTTP client。假设您的compileSdkVersion为23,请尝试将以下内容添加到build.gradle

android {
    compileSdkVersion 23
    useLibrary  'org.apache.http.legacy'
}