Skip to content

Instantly share code, notes, and snippets.

@parahall
Created September 21, 2016 12:36
Show Gist options
  • Save parahall/c1a2df70470a45e386d502e611b477e8 to your computer and use it in GitHub Desktop.
Save parahall/c1a2df70470a45e386d502e611b477e8 to your computer and use it in GitHub Desktop.
Mockito_problem
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'
import android.util.Log;
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();
}
}
}
import android.support.test.runner.AndroidJUnit4;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
/**
* Instrumentation test, which will execute on an Android device.
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
@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();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment