我正在尝试使用Android Room测试页面创建一个单元测试:https://developer.android.com/training/data-storage/room/testing-db.html
我的代码如下:
package com.example.repository;
import android.arch.persistence.room.Room;
import android.content.Context;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;
import com.example.repository.database.AppDatabase;
import com.example.repository.database.bloodpressure.BloodPressure;
import com.example.repository.database.bloodpressure.BloodPressureDao;
import junit.framework.Assert;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import java.time.Instant;
import java.util.List;
@RunWith(AndroidJUnit4.class)
public class BloodPressureInstrumentedTest {
private BloodPressureDao bloodPressureDao;
private AppDatabase appDatabase;
@Before
public void createDatabase() {
Context context = InstrumentationRegistry.getTargetContext();
appDatabase = Room.inMemoryDatabaseBuilder(context, AppDatabase.class).build();
bloodPressureDao = appDatabase.bloodPressureDao();
}
@After
public void closeDatabase() {
appDatabase.close();
}
@Test
public void writeBloodPressureAndRead() {
BloodPressure expectedBloodPressure = new BloodPressure(Instant.now(), 100, 80, 90);
bloodPressureDao.createBloodPressure(expectedBloodPressure);
List<BloodPressure> bloodPressures = bloodPressureDao.readAllBloodPressures();
Assert.assertEquals(expectedBloodPressure, bloodPressures.get(0));
}
}
当我尝试运行测试时,我总是得到以下错误:
Running tests
$ adb shell am instrument -w -r -e debug false -e class 'com.example.repository.BloodPressureInstrumentedTest#writeBloodPressureAndRead' com.example.repository.test/android.support.test.runner.AndroidJUnitRunner
Client not ready yet..
Started running tests
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.example.repository.database.AppDatabase.close()' on a null object reference
at com.example.repository.BloodPressureInstrumentedTest.closeDatabase(BloodPressureInstrumentedTest.java:37)
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 android.support.test.internal.runner.junit4.statement.RunAfters.evaluate(RunAfters.java:80)
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 android.support.test.runner.AndroidJUnit4.run(AndroidJUnit4.java:101)
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:56)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:384)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:2075)
Tests ran to completion.
如果运行@Before方法,则不应发生上述错误。这让我怀疑它根本没有运行,而是先运行@After。有没有遇到过这个?任何输入都将受到赞赏。
谢谢!
答案 0 :(得分:0)
显然@Before
在Room.inMemoryDatabaseBuilder(...)
崩溃了。如果日志中出现崩溃,那么本来会被发现更早(似乎是一个bug;在https://issuetracker.google.com/issues/78663445中报告了它。)
当@Before
崩溃时,它似乎绕过所有@Test
方法并执行@After
,因此出现此错误。
答案 1 :(得分:-1)
你在模块级build.gradle文件中有这个:
android {
defaultConfig {
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
}