1

我已经构建了一个 DAO 类来与 Android 中的 SQLiteDatabase 进行通信。当我在实际设备上运行一个简单的测试时,它可以工作。我能够将用户插入数据库并检索相同的用户。但是,当我使用 Robolectric 运行它时,我收到了 NullpointerException。这是 Robolectric 的问题吗?这是代码

public abstract class AbstractDAO<T> {

    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<User>{

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

而且测试...

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

}
4

0 回答 0