对于我的Android应用程序,我使用Realm移动数据库。我想为此编写测试,我发现测试的最佳方法是使用仪器测试。
这是我的数据库类的构造函数:
public Database(Context context) {
this.context = context.getApplicationContext();
Realm.init(context);
RealmConfiguration realmConfig = new RealmConfiguration.Builder()
.name("com.arnowouter.badger.realmdatabase") // TODO: 4/10/2016 check if encryptable
.build();
realm = Realm.getInstance(realmConfig);
}
如您所见,调用了Realm.init(context)。但是当我运行这个测试时:
@RunWith(AndroidJUnit4.class)
@MediumTest
public class DatabaseTest {
Database database;
Context context;
@Before
public void setupDatabase() {
context = InstrumentationRegistry.getContext();
database = new Database(context);
}
}
我收到此错误:
java.lang.IllegalStateException: Call `Realm.init(Context)` before creating a RealmConfiguration
at io.realm.RealmConfiguration$Builder.<init>(RealmConfiguration.java:399)
at io.realm.RealmConfiguration$Builder.<init>(RealmConfiguration.java:394)
at com.arnowouter.badger.Database.Database.<init>(Database.java:38)
at com.arnowouter.badger.DatabaseTest.setupDatabase(DatabaseTest.java:27)
at java.lang.reflect.Method.invoke(Native Method)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:59)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:262)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1970)
答案 0 :(得分:1)
考虑到Realm中存储的Context是这样的应用程序配置
// Thread pool for all async operations (Query & transaction)
volatile static Context applicationContext;
这是如此初始化
public static synchronized void init(Context context) {
if (BaseRealm.applicationContext == null) {
if (context == null) {
throw new IllegalArgumentException("Non-null context required.");
}
RealmCore.loadLibrary(context);
//...
BaseRealm.applicationContext = context.getApplicationContext();
}
}
这意味着您提供的context
会为null
返回context.getApplicationContext()
。
不幸的是,显然,检测的上下文会为null
返回getFilesDir()
,因此我们需要获取应用程序或活动上下文。
我可能只是在静态变量中设置应用程序,并使用它。
public class CustomApplication extends Application {
private static CustomApplication INSTANCE;
public static CustomApplication get() {
return INSTANCE;
}
@Override
public void onCreate() {
if(INSTANCE == null) {
INSTANCE = this;
}
}
}
在清单中:
<application name=".CustomApplication"
你可以这样做:
@RunWith(AndroidJUnit4.class)
@MediumTest
public class DatabaseTest {
Database database;
Context context;
@Before
public void setupDatabase() {
Realm.init(CustomApplication.get());
context = InstrumentationRegistry.getContext();
database = new Database(context);
}
}