views:

178

answers:

2

I'm trying to start unit testing on my application (should have done so from the beginning). I've got an Eclipse project structure set up and everything seems to work well, but...

I'm subclassing SQLiteOpenHelper to access the application database. This works well for the application, but when using the same class in the unit tests, it writes to the same database as the actual application. This is obviously rather irritating, since unit test data shows up when developing and testing the actual application.

What's the best way to make the SQLiteOpenHelper class write to a different database file when being called from the unit tests?

+2  A: 

You could consider to use the EasyMock library over at easymock.org to mock the SQLiteOpenHelpers subclass and its methods.

ubuntudroid
+1  A: 

If you don't have problems with adding some lines of code to your original program, you could also add a class variable and appropriate getter and setter methods to your Application subclass defining if you are in testing mode or not. Your unit test class could then access the setter method and set testing on "true". At the place in your code, where your database is defined, you would then access the getter method to decide which database to take.

ubuntudroid