我是新来的Android测试,我试图创建使用MockContext(以及实际上我想用一个重命名模拟上下文)的ApplicationTestCase。但我不断收到AssertionFailedError异常。这是我非常基本的code到目前为止:
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();
}
}
RenamingMockContext.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构造被注释掉(所以不要使用扩展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同时呼吁getShared preferences()的单元测试这对我没有工作,我也通过这本书的Android应用程序测试指南它举了一个例子正是这样的阅读,但是当我下载的源和运行它,直接它给了同样的错误。
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).
设置方法的几行加以注释,以获得测试工作:
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();
}
相关推荐
最新文章