Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
636 views
in Technique[技术] by (71.8m points)

unit testing - Android ApplicationTestCase using a MockContext

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

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);
        }

    }

}

Failure Trace:

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)

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.

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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).

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();
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...