Steven
Steven

Reputation: 207

Android instrumentation testing with mockito

My question is similar to this one. I am implementing instrumentation testing for my Android project. I want to test the integration by mocking my delegate, so that I can test if delegate methods are called as expected.

My gradle file:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"

    defaultConfig {
        applicationId "com.mytest"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    repositories {
        maven { url 'http://repo1.maven.org/maven2' }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:23.4.0';
    compile 'com.android.support:design:23.4.0';

    androidTestCompile 'com.google.dexmaker:dexmaker-mockito:1.2';
    androidTestCompile 'com.google.dexmaker:dexmaker:1.2';
    androidTestCompile 'org.mockito:mockito-core:1.10.19'
    androidTestCompile 'com.android.support:support-annotations:23.4.0';
    androidTestCompile 'com.android.support.test:runner:0.5';
    androidTestCompile 'com.android.support.test:rules:0.5';
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2';
}

My Test file:

public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActivity> {

    MainActivity mainActivity;

    @Mock
    MyDelegate delegate;

    public MainActivityTest() {
        super(MainActivity.class);
    }

    @Before
    public void setUp() throws Exception {
        mainActivity = getActivity();
        MockitoAnnotations.initMocks(this);
        mainActivity.setDelegate(delegate);
    }

    @Test
    public void testDelegateMethod1() throws Exception {
        mainActivity.doSomething();
        //delegate.method1 should be called
    }

}

I got the error:

java.lang.NullPointerException
at com.google.dexmaker.mockito.DexmakerMockMaker.getInvocationHandlerAdapter(DexmakerMockMaker.java:80)
at com.google.dexmaker.mockito.DexmakerMockMaker.getHandler(DexmakerMockMaker.java:75)
at org.mockito.internal.util.MockUtil.isMockitoMock(MockUtil.java:74)
at org.mockito.internal.util.MockUtil.isMock(MockUtil.java:66)
at org.mockito.internal.configuration.injection.scanner.MockScanner.isMockOrSpy(MockScanner.java:86)
at org.mockito.internal.configuration.injection.scanner.MockScanner.preparedMock(MockScanner.java:72)
at org.mockito.internal.configuration.injection.scanner.MockScanner.scan(MockScanner.java:61)
at org.mockito.internal.configuration.injection.scanner.MockScanner.addPreparedMocks(MockScanner.java:47)
at org.mockito.internal.configuration.InjectingAnnotationEngine.injectMocks(InjectingAnnotationEngine.java:96)
at org.mockito.internal.configuration.InjectingAnnotationEngine.processInjectMocks(InjectingAnnotationEngine.java:62)
at org.mockito.internal.configuration.InjectingAnnotationEngine.process(InjectingAnnotationEngine.java:56)
at org.mockito.MockitoAnnotations.initMocks(MockitoAnnotations.java:108)

Could anyone help with this issue?

Upvotes: 4

Views: 2729

Answers (2)

Steven
Steven

Reputation: 207

Not sure why

MockitoAnnotations.initMocks(this);

cause the crash, I end up with using

System.setProperty("dexmaker.dexcache", InstrumentationRegistry.getTargetContext().getCacheDir().getPath());

instead.

Upvotes: 1

N Jay
N Jay

Reputation: 1824

@Captor
private ArgumentCaptor<Callback> CallbackArgumentCaptor;

And then in your test method you will verify that it is called by doing the following:

verify(repository).retrieveSomething(callbackArgumentCaptor.capture());
callbackArgumentCaptor.getValue().successful(); 

or call failure depends on what you are testing. Don't forget to add this as well to the @Before

@Before
   public void setup() {
       MockitoAnnotations.initMocks(this);
   }

Upvotes: 0

Related Questions