@After 似乎先于其他一切执行

@After seems to be executed first before everything else

我正在尝试根据 Android 房间测试页面创建单元测试: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.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.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access[=11=]0(ParentRunner.java:58)
at org.junit.runners.ParentRunner.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.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access[=11=]0(ParentRunner.java:58)
at org.junit.runners.ParentRunner.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 首先是 运行。有没有人遇到过这个?任何输入将不胜感激。

谢谢!

你的模块级别 build.gradle 文件中有这个吗:

android {
    defaultConfig {
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
}

显然 @BeforeRoom.inMemoryDatabaseBuilder(...) 崩溃。如果该崩溃出现在日志中(似乎是一个错误;已在 https://issuetracker.google.com/issues/78663445 中报告),将会被更早地发现。

@Before 崩溃时,它似乎绕过所有 @Test 方法并执行 @After,因此出现此错误。