Android Studio 上的单元测试:“未模拟"错误 [英] Unit testing on Android Studio: "not mocked" error
问题描述
我是 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.
推荐答案
我无法让 Instrumentation 测试工作,使用 Android Studio,我猜他们仍在完成实施.由于它需要在模拟器上运行,因此有更快的选择:常规单元测试.
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但在不久的将来可能会再次弃用,因为我使用谷歌找到的有关此主题的所有信息都已弃用.
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屋!