Reputation: 2155
I am currently working on an android Room Persistency database project. I have successfully implemented all functions, and am now trying to write unittests for these function. These unittests rely on calling the following function (singleton constructor; I have marked the two important lines with comments):
@Database(entities = {RippleModel.class}, version = 1)
public abstract class AppDatabase extends RoomDatabase {
public abstract RippleModelDao rippleModelDao();
private static AppDatabase INSTANCE; //Used for the singleton
public static AppDatabase getDatabase(Context context) { //THIS LINE
if (INSTANCE == null) {
INSTANCE = Room.databaseBuilder(
context.getApplicationContext(),
AppDatabase.class, "ripple_db"
).build(); //STACKTRACE SHOWS HERE
}
return INSTANCE;
}
}
My current unittest looks as follows:
@RunWith(MockitoJUnitRunner.class)
public class DatabaseUnitTest extends InstrumentationTestCase {
@Mock
Context mMockContext = new MockContext();
@Test
public void testImportDatabase() throws Exception {
System.out.println("Importing database..");
System.out.println(mMockContext);
System.out.println(mMockContext == null);
AppDatabase appDatabase = AppDatabase.getDatabase(mMockContext);
}
}
However, when running this unittest with Mockito, I am getting the following error:
java.lang.IllegalArgumentException: Cannot provide null context for the database.
at android.arch.persistence.room.RoomDatabase$Builder.build(RoomDatabase.java:441)
at io.turtlesquad.ripplebt.data.AppDatabase.getDatabase(AppDatabase.java:20)
at io.turtlesquad.ripplebt.DatabaseUnitTest.testImportDatabase(DatabaseUnitTest.java:43)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
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.InvokeMethod.evaluate(InvokeMethod.java:17)
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.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnit45AndHigherRunnerImpl.java:37)
at org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:62)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMainV2.main(AppMainV2.java:131)
I already checked if the Mockito context is not null, using "== null", and this is not the case (the created context is not null).
I am trying to emulate the context with Mockito, but am not sure what I am doing wrong here. Shouldn't Mockito emulate the context? Is there anything I miss?
Upvotes: 1
Views: 1739
Reputation: 1864
I have solved the same issue by replacing context.getApplicationContext()
with context
from parameters of getDatabase(.....)
method of AppDatabase class
Replace your AppDaatabase java file with the following code:
@Database(entities = {RippleModel.class}, version = 1)
public abstract class AppDatabase extends RoomDatabase {
public abstract RippleModelDao rippleModelDao();
private static AppDatabase INSTANCE; //Used for the singleton
public static AppDatabase getDatabase(Context context) { //THIS LINE
if (INSTANCE == null) {
INSTANCE = Room.databaseBuilder(
context,
AppDatabase.class, "ripple_db"
).build(); //STACKTRACE SHOWS HERE
}
return INSTANCE;
}
}
Upvotes: 6