Mockito测试框架在Android中不起作用

时间:2016-12-16 15:07:20

标签: android unit-testing testing mockito

我开始为我的应用程序编写单元测试,并且在Android Studio中遇到Mockito功能问题。例如,我无法模拟Context对象。这是我的基本代码:

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

在我的应用级 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()方法功能?

2 个答案:

答案 0 :(得分:1)

我相信它是因为你并没有真正使用模拟。

  

在测试类中检测到不必要的stubbings:ExampleUnitTest

     

清洁&可维护的测试代码需要零代码。

答案 1 :(得分:0)

这些不必要的存根是在测试执行期间从未实现的存根方法调用。 您可以编写:

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

可以使用Silent这样的选项来避免相同的情况

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

}

Developer会添加不必要的测试代码。为了保持代码库干净,必须删除不必要的代码。