Я пытаюсь создать шаблон юнит-теста со страницы тестирования 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. Кто-нибудь сталкивался с этим? Любые вклады будут оценены.
Спасибо!