Issue
I have an OrmLiteBaseActivity and in the onCreate() of this activity i read some data from a database. Depending on the data read I change the visibility of some views.
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. I want to test the visibility of the views depending on the data read.
Any ideas, guides, examples or anything else that will help me is welcomed :)
Solution
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:
ConnectionSourcewhich gives connections to the databaseDatabaseConnectionwhich represents the connectionCompiledStatementwhich represents a compiled SQL statementDatabaseResultswhich gives returns fromCompiledStatement.runQuery()
When you construct your DAOs, you pass in a ConnectionSource implementation that gives DatabaseConnections, 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 theConnectionSourceandDatabaseConnectionbut doesn't go all of the way toDatabaseResults.TableUtilsTest.testStatement()-- it shows some complex mocking ofDatabaseConnection,CompiledStatement, andDatabaseResults.H2CompiledStatementand other classes in that test package. This is the test code that ORMLite uses to test the various database operations. H2++.
Answered By - Gray
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.