title: Database Test Robolectric Not Working
body: <p>I have built a DAO class to communicate with the SQLiteDatabase in Android. When I run a simple test on the actual device it works. I am able to insert a user into the db and retrieve the same user. However when I run this with Robolectric I receive a NullpointerException. Is this a problem with Robolectric? Heres the code</p>
  
  <pre><code>public abstract class AbstractDAO&lt;T&gt; {
  
      private SQLiteDatabase database;
      private String tableName;
  
      @Inject
      public AbstractDAO(SQLiteDatabase database, String tableName) {
          this.database = database;
          this.tableName = tableName;
      }
  
      public T get(String id) {
          Cursor cursor = database.rawQuery("SELECT * FROM " + tableName, null);
          cursor.moveToFirst();
          return cursorToObject(cursor);
      }
  
      public void insert(T object) {
          database.insert(tableName, null, toContentValues(object));
      }
  
      public Cursor get() {
          return database.rawQuery("SELECT * FROM " + tableName, null);
      }
  
      protected abstract ContentValues toContentValues(T object);
  
      protected abstract T cursorToObject(Cursor cursor);
  }
  
  
  
  public class UserDAO extends AbstractDAO&lt;User&gt;{
  
      private static final String TABLE_NAME = "users";
      private static final String KEY_ID = "_id";
      private static final String KEY_FIRST_NAME = "FirstName";
      private static final String KEY_LAST_NAME = "LastName";
      private static final String KEY_FACEBOOK_ID = "FacebookId";
      private static final String KEY_FACEBOOK_USERNAME = "FacebookUserName";
  
      @Inject
      public UserDAO(SQLiteDatabase database) {
          super(database, TABLE_NAME);
      }
  
      @Override
      protected ContentValues toContentValues(User user) {
          ContentValues contentValues = new ContentValues();
          contentValues.put(KEY_ID, user.getId());
          contentValues.put(KEY_FIRST_NAME, user.getFirstName());
          contentValues.put(KEY_LAST_NAME, user.getLastName());
          contentValues.put(KEY_FACEBOOK_ID, user.getFacebookId());
          contentValues.put(KEY_FACEBOOK_USERNAME, user.getFacebookUserName());
          return contentValues;
      }
  
      @Override
      protected User cursorToObject(Cursor cursor) {
          User user = new User();
          user.setId(cursor.getString(cursor.getColumnIndexOrThrow(KEY_ID)));
          user.setFirstName(cursor.getString(cursor.getColumnIndexOrThrow(KEY_FIRST_NAME)));
          user.setLastName(cursor.getString(cursor.getColumnIndexOrThrow(KEY_LAST_NAME)));
          user.setFacebookId(cursor.getString(cursor.getColumnIndexOrThrow(KEY_FACEBOOK_ID)));
          user.setFacebookUserName(cursor.getString(cursor.getColumnIndexOrThrow(KEY_FACEBOOK_USERNAME)));
          return user;
      }
  </code></pre>
  
  <p>And the test...</p>
  
  <pre><code>@RunWith(RoboInjectedTestRunner.class)
  public class UserDAOTest {
  
      @Inject
      private UserDAO userDAO;
  
      @Test
      public void shouldSaveUser() {
          //WORKING ON DEVICE BUT NOT IN TEST
          User user = new User();
          user.setFirstName("Damu");
          user.setLastName("Tow");
          user.setId("12345");
          user.setFacebookId("54321");
          user.setFacebookUserName("Damow Ting");
          userDAO.insert(user);
          User user1 = userDAO.get("12345");
          Assert.assertEquals("12345", user1.getId());
          Assert.assertEquals("Damu", user1.getFirstName());
          Assert.assertEquals("Tow", user1.getLastName());
          Assert.assertEquals("54321", user1.getFacebookId());
          Assert.assertEquals("Damow Ting", user1.getFacebookUserName());
      }
  
  }
  </code></pre>