title: Android, Room fail on unit testing with Robolectric
body: <p>I am trying to create some tests for a view model. </p>
  
  <p>The view model includes a Database instantiation which calls a Database</p>
  
  <p><code>CallRoomDatabase db = CallRoomDatabase.getDatabase(application);</code></p>
  
  <p>where getDatabase takes an instance of the Dao()</p>
  
  <pre><code>  @Database(entities = {CallEntity.class}, version = 1)
   public abstract class CallRoomDatabase extends RoomDatabase {
  
  public abstract CDao cDao();
  
  // marking the instance as volatile to ensure atomic access to the variable
  private static volatile CRoomDatabase INSTANCE;
  
  public static CRoomDatabase getDatabase(final Context context) {
      if (INSTANCE == null) {
          synchronized (CallRoomDatabase.class) {
              if (INSTANCE == null) {
                  INSTANCE = Room.databaseBuilder(context.getApplicationContext(),
                          CRoomDatabase.class, "database")
                          // Wipes and rebuilds instead of migrating if no Migration object.
                          // Migration is not part of this codelab.
                          .fallbackToDestructiveMigration()
                          .addCallback(sRoomDatabaseCallback)
                          .build();
              }
          }
      }
      return INSTANCE;
  }
  </code></pre>
  
  <p>However, when trying to instantiate the model within a test, I get an error on <code>.build()</code> above</p>
  
  <pre><code>  java.lang.NullPointerException
  at androidx.room.Room.getGeneratedImplementation(Room.java:79)
  at androidx.room.RoomDatabase$Builder.build(RoomDatabase.java:952)
  at com.s.o.dbutils.CRoomDatabase.getDatabase(CRoomDatabase.java:32)
  at com.s.o.viewmodels.CViewModelTest.checkForNuTest(CViewModelTest.kt:66)
  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.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
  at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
  at org.powermock.modules.junit4.rule.PowerMockStatement$1.run(PowerMockRule.java:83)
  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)
  </code></pre>
  
  <p>At <code>RoomDatabase.java</code> </p>
  
  <pre><code>            T db = Room.getGeneratedImplementation(mDatabaseClass, DB_IMPL_SUFFIX);
  </code></pre>
  
  <p>At <code>Room.java</code>  <code>klass</code> is <code>null</code></p>
  
  <pre><code>        static &lt;T, C&gt; T getGeneratedImplementation(Class&lt;C&gt; klass, String suffix) {
  </code></pre>
  
  <p>I have tried not using the model at all at the test, just instantiate the db with</p>
  
  <pre><code>          val db = CRoomDatabase.getDatabase(ApplicationProvider.getApplicationContext())
  </code></pre>
  
  <p>Still breaks at the same <code>.build()</code></p>
  
  <p>Any ideas how to get past that exception ?</p>
  
  <p>We dont actually need to test the db someway, just some functions included in that model, so some way to just avoid the error would suffice.</p>