Testing class communicating with the database via ORMLite DAO

I am trying to use the TDD approach when creating an Android application. I use ORMLite and Mockito / Robolectric for testing. I ran into the problem of testing a simple thing:

(a method in some class wrapping DAO calls)

public List<ITask> getTasksForNextTwoWeeks() throws SQLException {
    // Code to be written
}

Well, the code inside will be the correct call to the request method.

What is the best approach to check this code? I thought about this, but I can’t come up with a solution without access to a real database (either real or test).

Any suggestions are welcome.

+5
source share
2 answers

. , Dao. ORMLite Dao , , DAO mock.

, setDao sorta :

public void setDao(Dao<ITask, String> dao) {
    this.dao = dao;
}

private Dao<ITask, String> getDao() {
    if (dao != null) {
       // typical ORMLite pattern
       dao = getHelper().getITaskDao();
    }
    return dao;
}

getTasksForNextTwoWeeks() - :

public List<ITask> getTasksForNextTwoWeeks() throws SQLException {
    QueryBuilder<ITask, String> qb = getDao().getQueryBuilder();
    qb.where().gt(...);
    return qb.query();
}

, QueryBuilder.

, ORMLIte Dao getTasksForNextTwoWeeks() ITaskDao.

public interface ITaskDao extends Dao<ITask, String> {
   public List<ITask> getTasksForNextTwoWeeks() throws SQLException;
   ...
}

ITaskDao .

, .

+2

, . , null :

OrmLiteSqliteOpenHelper(context,null, null, DATABASE_VERSION );

, ) b) , SqliteOpenHelper -wrapper

.

+4

All Articles