Android ApplicationTestCase使用MockContext

时间:2013-01-07 22:46:30

标签: android unit-testing mocking android-context

我是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应用程序测试指南”一书,该指南给出了一个与此类似的示例,但是当我下载源代码并运行它时直接它给出了同样的错误。

2 个答案:

答案 0 :(得分:4)

作为该书样本的解决方法,请查看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();
    }

答案 1 :(得分:2)

我使用AndroidTestCase来模拟一个简单的上下文。

class ExampleTest extends AndroidTestCase
    public void setUp() {
      Context c = new DelegatedMockContext(getContext())
    }

    class DelegatedMockContext extends MockContext {

    private Context mDelegatedContext;
        private static final String PREFIX = "test.";

        public DelegatedMockContext(Context context) {
             mDelegatedContext = context;
        }

        @Override
        public String getPackageName(){
            return PREFIX;
        }

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

它只是一个沼泽标准的上下文,但会让你前进