Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing class communicating with DB through ORMLite's DAO

I'm trying to take a TDD approach in creating Android app. I'm using ORMLite and Mockito/Robolectric for testing. I've run into trouble testing a simple thing:

(method in some class wrapping up DAO calls)

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

Well, the code inside will be just a proper query method call.

What's the best approach to test that code? I've been thinking about this, but can't think of solution without accessing the real database (whether real or test one).

Any suggestions welcome.

like image 387
LordTwaroog Avatar asked Jan 06 '12 21:01

LordTwaroog


1 Answers

I am not fan of Gray's response, since it makes things little bit too complicated. I recommend you to simply create in-memory database instead, by passing null as a database name:

OrmLiteSqliteOpenHelper(context,null, null, DATABASE_VERSION );

That way you can test your queries in single test by a) adding mock elements b) testing if your SqliteOpenHelper-wrapper returns correct results

Every test like that is completely independent from your actual database and other tests in your suite.

like image 65
Piotr Avatar answered Nov 15 '22 03:11

Piotr