title: Mockito testing framework not working in Android
body: <p>I am starting to write unit tests for my application and have problem with <code>Mockito</code> functionality in Android Studio. For example I can't mock <code>Context</code> object. Here is my very basic code:</p>
  
  <p><strong>ExampleUnitTest.java</strong> class:</p>
  
  <pre><code>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");
      }
  }
  </code></pre>
  
  <p>In my app level <strong>build.gradle</strong> file I have such dependencies:</p>
  
  <pre><code>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"
  }
  </code></pre>
  
  <p>So according to all tutorials, etc. everything should be OK. But when I try to run my test, I get such exception:</p>
  
  <pre><code>org.mockito.exceptions.misusing.UnnecessaryStubbingException: 
  Unnecessary stubbings detected in test class: ExampleUnitTest
  Clean &amp; maintainable test code requires zero unnecessary code.
  Following stubbings are unnecessary (click to navigate to relevant line of code):
    1. -&gt; 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)
  </code></pre>
  
  <p>What I am doing wrong here that I can't mock simple <code>Context</code> object <code>getString()</code> method functionality?</p>