测试GreenDao实体,QueryBuilder始终为空

时间:2015-11-18 06:29:34

标签: android junit greendao

我正在尝试在android中测试greenDao实体,我无法弄清楚如何正确获取dao对象的副本。

我正在尝试:

@Before
public void setUp() throws Exception {

    user = new User(id, userId, email, password, token);

    Context context = new MockContext();
    DaoMaster.DevOpenHelper openHelper = new DaoMaster.DevOpenHelper(context , null, null);
    SQLiteDatabase db = openHelper.getWritableDatabase();
    daoSession = new DaoMaster(db).newSession();
    userDb = daoSession.getUserDao();

    assertNotNull(userDb); // This PASSES

    userDb.queryBuilder().list(); // This ALWAYS throws null pointer exception
}

有什么建议吗?我的测试类扩展了AndroidTestCase

更新

我已经研究了如何使用以下方法在设备上运行测试时访问实体:

public class UserDaoHelperTest extends ApplicationTestCase<Application> {

    private UserDao userDb;

    public UserDaoHelperTest() {
        super(Application.class);
    }

    @Override
    public void setUp(){

        DaoMaster.DevOpenHelper openHelper = new DaoMaster.DevOpenHelper(getContext(), null, null);
        SQLiteDatabase db = openHelper.getWritableDatabase();
        DaoSession daoSession = new DaoMaster(db).newSession();
        userDb = daoSession.getUserDao();
    }
}

1 个答案:

答案 0 :(得分:0)

我已经研究了如何使用以下方法在设备上运行测试时访问实体:

public class UserDaoHelperTest extends ApplicationTestCase<Application> {

    private UserDao userDb;

    public UserDaoHelperTest() {
        super(Application.class);
    }

    @Override
    public void setUp(){

        DaoMaster.DevOpenHelper openHelper = new DaoMaster.DevOpenHelper(getContext(), null, null);
        SQLiteDatabase db = openHelper.getWritableDatabase();
        DaoSession daoSession = new DaoMaster(db).newSession();
        userDb = daoSession.getUserDao();
    }
}