我有一個內部類類如下:間諜的Mockito - 調用內部類方法時不刺探方法諜對象
public class ClassWithInnerObject {
private final InnerObject innerObject;
public ClassWithInnerObject() {
innerObject = new InnerObject();
}
public void callInnerObjectMethod() {
innerObject.outerFunc();
}
public void outerFunc() {
innerFunc();
}
public void innerFunc() {
Log.d("XXX", "innerFunc: called");
}
public class InnerObject {
public void outerFunc() {
innerFunc();
}
}
}
而且測試的Mockito正在尋找如下: 的build.gradle:
androidTestCompile 'junit:junit:4.12'
androidTestCompile 'org.mockito:mockito-core:1.10.19'
androidTestCompile 'com.crittercism.dexmaker:dexmaker:1.4'
androidTestCompile 'com.crittercism.dexmaker:dexmaker-mockito:1.4'
androidTestCompile 'com.crittercism.dexmaker:dexmaker-dx:1.4'
測試:
@RunWith(AndroidJUnit4.class) public class SpyVerifyTest {
@Test public void myInnerTestWorking() {
ClassWithInnerObject p = new ClassWithInnerObject();
ClassWithInnerObject spy = Mockito.spy(p);
spy.outerFunc();
verify(spy, times(1)).innerFunc();
}
@Test public void myInnerTestNotWorking() {
ClassWithInnerObject p = new ClassWithInnerObject();
ClassWithInnerObject spy = Mockito.spy(p);
spy.callInnerObjectMethod();
verify(spy, times(1)).innerFunc();
}
}
第一個測試工作正常。 第二個innerFunc
永遠不會被檢測爲「被調用」,雖然在日誌中我看到它是。什麼是錯的? :)
謝謝!
我覺得這個問題已經是另一種情況了: http://stackoverflow.com/a/22644040/5627827 –