2015-10-15 64 views
2

我正在爲使用MFP 7.1的Android項目編寫單元測試的過程,我試圖測試一些將WLResponse對象作爲輸入的功能。我想嘲笑這個對象,因爲我需要的唯一東西是JSONObject,它通過調用getResponseJSON方法返回。Mocking WLResponse爲HttpResponse拋出ClassNotFoundException

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嘲笑它。

回答

相關問題