@After seems to be executed first before everything else

Karl Jamoralin

I'm trying to create a unit test patterned from the Android Room testing page: https://developer.android.com/training/data-storage/room/testing-db.html

My code is as follows:

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));
    }

}

When I try to run the test, I always get the below error:

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.

The above error shouldn't happen if the @Before method was ran. This makes me suspect that it's not being run at all, and instead @After is being run first. Has anyone encountered this? Any inputs will be appreciated.

Thanks!

Karl Jamoralin

Apparently @Before is crashing at Room.inMemoryDatabaseBuilder(...). Would have been caught much earlier if that crash appeared in the logs (seems to be a bug; reported it in https://issuetracker.google.com/issues/78663445).

When @Before crashes, it appears to bypass all @Test methods and executes @After, hence this error.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How keep first character but remove everything else before space in R?

Will be executed everything which is into parenthesis first?

Removing everything after/before the first occurrence of a given string

SED or AWK to remove everything before the first dash and after the last

PHP: Remove everything before the first '-'

Second update is executed before the first

functions in modules are getting executed first and print statements are not getting executed even after placing them first(Before the function call)

Get everything before and after a character

Deleting everything before and after matches

Spring - register Beans before everything else

Load a JS File before everything else in MeteorJS

UnicodeFont rendering seems to block everything else from rendering?

How to remove everything after first instance of specific delimiter, then before last instance of specific delimiter using Regex? - SQL

PowerShell - Get everything after the first "-"

keep the first part of the URL and mask everything else

remove everything before first pipe using regex

Regex - get everything before first comma - python

Regular expression: Replace everything before first occurence

Remove everything before first occurence of specified word

Regex/PHP Remove everything before first letter

regex to get everything before first number

How to extract everything before the first space?

Regex - Delete everything before first match

Delete everything in a string before the first period in excel

Remove everything before the first space in R

jq value remove everything before first space

Azure CLI - VSTS - Yaml - Only the first line of the script seems to be executed

Maven rename a file after everything else finishes

Find value in String and delete it an everything else after it