使用 MockContext 的 Android ApplicationTestCase [英] Android ApplicationTestCase using a MockContext

查看:31
本文介绍了使用 MockContext 的 Android ApplicationTestCase的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Android 测试的新手,我正在尝试使用 MockContext 创建 ApplicationTestCase(实际上我正在尝试使用重命名模拟上下文).但我不断收到 AssertionFailedError.到目前为止,这是我非常基本的代码:

I'm new to Android testing and I'm trying to create an ApplicationTestCase using a MockContext (well actually I'm trying to use a Renaming Mock Context). But I keep getting an AssertionFailedError. Here's my very basic code so far:

AppTests.java

AppTests.java

package com.myProject.test;

import android.test.ApplicationTestCase;

public class AppTests extends ApplicationTestCase<MyApplication> {
    public AppTests() {
        super(MyApplication.class);
    }

    @Override
    protected void setUp() throws Exception {
        final RenamingMockContext mockContext = new RenamingMockContext(getContext());
        setContext(mockContext);
        createApplication();
    }

}

MyApplication.java

MyApplication.java

package com.myProject.test;

import android.app.Application;

public class MyApplication extends Application {

    public MyApplication() {
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

}

重命名MockContext.java

RenamingMockContext.java

package com.myProject.test;

import android.content.Context;
import android.content.SharedPreferences;
import android.test.RenamingDelegatingContext;
import android.test.mock.MockContext;

public class RenamingMockContext extends RenamingDelegatingContext {

    private static final String PREFIX = "test.";

    public RenamingMockContext(Context context) {
        super (new DelegatedMockContext(context), PREFIX);
        //super(context, PREFIX);
    }

    private static class DelegatedMockContext extends MockContext {
        private Context mDelegatedContext;
        public DelegatedMockContext(Context context) {
            mDelegatedContext = context;
        }

        @Override
        public String getPackageName() {
            return mDelegatedContext.getPackageName();
        }

        @Override
        public SharedPreferences getSharedPreferences(
          String name, int mode) {
          return mDelegatedContext.getSharedPreferences(
            PREFIX + name, mode);
        }

    }

}

故障跟踪:

junit.framework.AssertionFailedError
at android.test.ApplicationTestCase.setupApplication(ApplicationTestCase.java:102)
at android.test.ApplicationTestCase.createApplication(ApplicationTestCase.java:118)
at com.myApplication.test.AppTests.setUp(AppTests.java:14)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:190)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:175)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:555)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1710)

请注意,如果我在被注释掉的 RenamingMockContext 构造函数中使用第二个 super() 调用(所以不要使用扩展的 MockContext 类),它可以正常工作.

Note if I use the second super() call in the RenamingMockContext constructor that is commented out (so don't use the extended MockContext class) it works fine.

这是一个参考UnsupportedOperationException while call getSharedPreferences() from unit test 这对我不起作用,我还通读了 Android Application Testing Guide 一书,它给出了一个完全一样的例子,但是当我下载源代码并直接运行它时,它给出了同样的错误.

Here is a reference UnsupportedOperationException while calling getSharedPreferences() from unit test which didn't work for me, and I also read through the book Android Application Testing Guide which gives an example exactly like this, but when I downloaded the source and ran it directly it gave the same error.

推荐答案

作为该书示例的解决方法,请查看 Android 开发人员指南 ApplicationTestCase:如果简单地按原样运行测试,您的应用程序将被注入全功能上下文" (http://developer.android.com/reference/android/test/ApplicationTestCase.html).

As a workaround for that book sample, check the android developer guide to ApplicationTestCase: "If simply run your tests as-is, your Application will be injected with a fully-functional Context" (http://developer.android.com/reference/android/test/ApplicationTestCase.html).

必须注释几行 Setup 方法才能使测试正常工作:

A few lines of the Setup method must be commented to get the test working:

protected void setUp() throws Exception
    {
        super.setUp();
        // final RenamingMockContext mockContext = new RenamingMockContext(
        // getContext());
        // setContext(mockContext);

        createApplication();
        mApplication = getApplication();
    }

这篇关于使用 MockContext 的 Android ApplicationTestCase的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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