android - Android Studio : "not mocked" error 单元测试

标签 android unit-testing android-studio

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

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

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 中:

testCompile 'junit:junit:4.12'

我试过添加这个

testOptions { 
    unitTests.returnDefaultValues = true
}

因为我在 http://tools.android.com/tech-docs/unit-testing-support 遵循的步骤中提到了这一点 但它并没有解决它。

我还尝试创建一个 MockContext:

preferences = new AppPreferences(new MockContext());

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

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

...

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

最佳答案

我看到你已经更新了你的问题。

请查看此SharedPreferencesMockContext.java 的源代码:https://github.com/applicake/Beandroid/blob/master/Beanstalk%20Android%20ClientTest/src/com/applicake/beanstalkclient/test/SharedPreferencesMockContext.java .

这是测试: https://github.com/applicake/Beandroid/blob/master/Beanstalk%20Android%20ClientTest/src/com/applicake/beanstalkclient/test/NotificationsTests.java

这是一个 fragment ,展示了他们如何创建他们的 Mock:

  @Override
  protected void setUp() throws Exception {

    final SharedPreferencesMockContext mockContext = new SharedPreferencesMockContext(getContext());
    MockApplication mockApplication = new MockApplication(){
      @Override
      public Context getApplicationContext() {
        Log.d("tests", "Im here");
        return mockContext;
      }
    };


    context = mockContext;
    setApplication(mockApplication);
    prefs = PreferenceManager.getDefaultSharedPreferences(context);
    prefs.edit().clear().commit();

    super.setUp();
  }

昨晚我遇到了这个错误。尝试使用“MockContext”。

public class AppPreferencesTest extends InstrumentationTestCase {

AppPreferences preferences;
Context context;

@Before
public void setUp() throws Exception {
    context = new MockContext();
    preferences = new AppPreferences(context);
}

请在此处查看其他示例:https://stackoverflow.com/a/29063736/950427

关于android - Android Studio : "not mocked" error 单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29100925/

相关文章:

java - Android Studio 中的 Gradle 构建失败

java - 在Android Studio上使用TestNG运行演示程序

php - 将 GCM 迁移到 FCM,我应该/可以保留我当前的 GCM 服务器端代码吗?

android - 如何在二维风格中指定单元测试文件夹

javascript - karma : Cannot find module in relative path

javascript - Karma 错误 : [$injector:modulerr] Failed to instantiate module due to: (Index. html 工作正常)

java - Android:为什么后续代码阻止 TextView 的 .setText() 工作

android - 从 Android 中的 URL 获取目录列表

android - 使用 Observable Zip 行为不当

android - 在服务类中显示 ProgressDialog