测试 GreenDao 实体,QueryBuilder 始终为 Null

Testing GreenDao Entities, QueryBuilder is always Null

我目前正在尝试在 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
}

有什么建议吗?我的测试 class 扩展 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();
    }
}

我已经弄清楚如何在 运行 使用以下方法在设备上进行测试时访问实体:

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