在Android Studio上进行单元测试:“未模拟"错误 [英] Unit testing on Android Studio: "not mocked" error

查看:224
本文介绍了在Android Studio上进行单元测试:“未模拟"错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Android Studio的新手.我正在使用Android Studio 1.2预览版2,gradle 2.2.1和gradle插件1.1.0.

I am new to Android Studio. I am using Android Studio 1.2 preview 2, gradle 2.2.1 and gradle plugin 1.1.0.

尝试运行单元测试时,我无法解决此错误:

I cannot get around this error, when trying to run my unit tests:

java.lang.RuntimeException: Method getInstrumentation in android.test.InstrumentationTestCase not mocked

这是我的测试课:

public class AppPreferencesTest extends InstrumentationTestCase {

AppPreferences preferences;

@Before
public void setUp() throws Exception {
    preferences = new AppPreferences(getInstrumentation().getTargetContext());
}

...

在我的build.gradle中:

In my build.gradle:

testCompile 'junit:junit:4.12'

我尝试添加此内容

testOptions { 
    unitTests.returnDefaultValues = true
}

因为我在 http://tools遵循的步骤中已提到.android.com/tech-docs/unit-testing-support 但它不能解决问题.

because that was mentioned in the steps that I followed at http://tools.android.com/tech-docs/unit-testing-support but it does not fix it.

我还尝试创建一个MockContext:

I also tried creating a MockContext:

preferences = new AppPreferences(new MockContext());

但是AppPreferences的构造函数却给出了错误

but the constructor of AppPreferences than gives an error

public AppPreferences(Context context) {
    preferences = PreferenceManager.getDefaultSharedPreferences(
            context);
}

...

RuntimeException: Method getDefaultSharedPreferences in android.preference.PreferenceManager not mocked.

推荐答案

使用Android Studio,我无法使Instrumentation测试正常工作,我想它们仍在完成实现.并且由于它需要在仿真器上运行,因此有更快的选择:常规的单元测试.

I couldn't get Instrumentation tests to work, using Android Studio, I guess they're still finalising the implementation. And since it needs to run on the emulator, there are faster options: regular unit tests.

多亏了Jared的提示,我切换到了Robolectric,它易于在Android Studio上使用.

Thanks to Jared's tips, I switched to Robolectric, which is easy to use on Android Studio.

androidTestCompile 'junit:junit:4.12'
androidTestCompile "org.robolectric:robolectric:3.0"

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.RobolectricTestRunner;
import static junit.framework.TestCase.assertEquals;

@RunWith(RobolectricTestRunner.class)
public class AppPreferencesTest {

    AppPreferences preferences;

    @Before
    public void setUp() throws Exception {
        preferences = new AppPreferences(RuntimeEnvironment.application.getApplicationContext());
    }

    @Test
    public void testIsNotificationsEnabled_Default() throws Exception {
        assertEquals(true, preferences.isNotificationsEnabled());
    }

    ...

此处的信息目前看来是正确的: http://nenick-android.blogspot .nl/2015/02/android-studio-110-beta-4-and.html 但由于我使用google找到的有关此主题的所有信息都已弃用,因此在不久的将来可能会再次弃用.

The info here seems to be correct at this time: http://nenick-android.blogspot.nl/2015/02/android-studio-110-beta-4-and.html But will probably deprecate again in the near future, as all the info I found on this subject using google deprecated already.

这篇关于在Android Studio上进行单元测试:“未模拟"错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆