Android ApplicationTestCase使用MockContext

前端之家收集整理的这篇文章主要介绍了Android ApplicationTestCase使用MockContext前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我是 Android测试的新手,我正在尝试使用MockContext创建一个ApplicationTestCase(实际上我正在尝试使用重命名模拟上下文).但我不断收到AssertionFailedError.到目前为止,这是我的基本代码

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

package com.myProject.test;

import android.app.Application;

public class MyApplication extends Application {

    public MyApplication() {
    }

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

}

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类)它可以正常工作.

这是一个对我来说不起作用的参考UnsupportedOperationException while calling getSharedPreferences() from unit test,我还读了一下Android应用程序测试指南这本书,它给出了一个完全相同的例子,但是当我下载源代码并直接运行它时它给出了同样的错误.

解决方法

作为该书样本的解决方法,请查看ApplicationTestCase的android开发人员指南:“如果只是按原样运行测试,那么将为您的应用程序注入一个功能齐全的上下文”( http://developer.android.com/reference/android/test/ApplicationTestCase.html).

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

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

        createApplication();
        mApplication = getApplication();
    }
原文链接:https://www.f2er.com/android/315890.html

猜你在找的Android相关文章