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

问题描述 投票:0回答:2

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

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()方法功能?

android unit-testing testing mockito
2个回答
1
投票

我相信这是因为你实际上并没有使用模拟器。

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

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


0
投票

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

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

使用Silent选项可以避免相同

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

}

开发人员添加了不必要的测试代为了保持代码库清洁,有必要删除不必要的代码。

© www.soinside.com 2019 - 2024. All rights reserved.