user2999943
user2999943

Reputation: 2519

Mockito testing framework not working in Android

I am starting to write unit tests for my application and have problem with Mockito functionality in Android Studio. For example I can't mock Context object. Here is my very basic code:

ExampleUnitTest.java class:

package com.mypackage;

import android.content.Context;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public class ExampleUnitTest {

    @Mock Context context;

    @Test
    public void test() throws Exception {
        when(context.getString(any(Integer.class))).thenReturn("Test");
    }
}

In my app level build.gradle file I have such dependencies:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.0.1'
    compile 'com.android.support:design:25.0.1'
    compile 'com.google.android.gms:play-services:10.0.1'
    testCompile 'junit:junit:4.12'
    testCompile 'org.mockito:mockito-core:2.1.0'
    compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
}

So according to all tutorials, etc. everything should be OK. But when I try to run my test, I get such exception:

org.mockito.exceptions.misusing.UnnecessaryStubbingException: 
Unnecessary stubbings detected in test class: ExampleUnitTest
Clean & maintainable test code requires zero unnecessary code.
Following stubbings are unnecessary (click to navigate to relevant line of code):
  1. -> at com.gapps.trailplanner.ExampleUnitTest.test(ExampleUnitTest.java:20)
Please remove unnecessary stubbings. More info: javadoc for UnnecessaryStubbingException class.

    at org.mockito.internal.exceptions.Reporter.formatUnncessaryStubbingException(Reporter.java:838)
    at org.mockito.internal.junit.UnnecessaryStubbingsReporter.validateUnusedStubs(UnnecessaryStubbingsReporter.java:30)
    at org.mockito.internal.runners.StrictRunner.run(StrictRunner.java:45)
    at org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:104)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:119)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

What I am doing wrong here that I can't mock simple Context object getString() method functionality?

Upvotes: 0

Views: 1799

Answers (2)

Gulzar Bhat
Gulzar Bhat

Reputation: 1327

These Unnecessary stubs are stubbed method calls that are never realized during test execution. You can write:

@Test
    public void test() throws Exception {
       context.getString(any(Integer.class))
    }

Same can be avoided using Silent option like

@RunWith(MockitoJUnitRunner.Silent.class)
public class ExampleUnitTest {

}

Developer adds up unnecessary test code. In order to keep the codebase clean it is necessary to remove unnecessary code.

Upvotes: 0

StuStirling
StuStirling

Reputation: 16211

I believe it's because you're not actually using the mock.

Unnecessary stubbings detected in test class: ExampleUnitTest

Clean & maintainable test code requires zero unnecessary code.

Upvotes: 1

Related Questions