2016-12-16 111 views
0

不工作我開始寫單元測試我的應用程序,並有問題,Android Studio中Mockito功能。比如我不能嘲笑Context的對象。這是我非常基本的代碼:測試的Mockito框架的Android

ExampleUnitTest.java類:

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"); 
    } 
} 

在我的應用程序級的build.gradle文件我也有這樣的依賴關係:

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" 
} 

所以根據所有的教程,等等一切都應該沒問題。但是,當我嘗試運行我的測試,我得到這樣的例外:

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) 

我在做什麼錯在這裏,我不能嘲笑簡單Context對象getString()方法的功能?

+0

異常追蹤的前六行似乎很清楚。 –

回答

1

我相信這是因爲你沒有實際使用的模擬。在測試類檢測

不必要stubbings:ExampleUnitTest

清潔&維護的測試代碼需要零不必要的代碼。