I'm writing tests for this activity. How can I mock the database (or even use another test database) so I can change the data in the database according to my tests needs so I can test different cases depending on that data.
All of the database stuff is driven internally to ORMLite with 4 interfaces:
ConnectionSource
which gives connections to the databaseDatabaseConnection
which represents the connectionCompiledStatement
which represents a compiled SQL statementDatabaseResults
which gives returns fromCompiledStatement.runQuery()
When you construct your DAOs, you pass in a ConnectionSource
implementation that gives DatabaseConnection
s, etc.. You can certain use EasyMock or some little concrete class to mock out these 4 interfaces. It's a little complex to do so but it can be done.
The unit tests for ORMLite are pretty extensive. For some partial examples you can take a look at:
FieldTypeTest.testForeignAutoRefresh()
-- it shows the mocking of theConnectionSource
andDatabaseConnection
but doesn't go all of the way toDatabaseResults
.TableUtilsTest.testStatement()
-- it shows some complex mocking ofDatabaseConnection
,CompiledStatement
, andDatabaseResults
.H2CompiledStatement
and other classes in that test package. This is the test code that ORMLite uses to test the various database operations. H2++.