1

I am new to testing in general and trying to figure out an acceptable strategy for testing the Sqlite database that I'm using on Android.

I'm struggling with how exactly I should do this and the merits of different methods.

Without much experience it seems like a good method might be to run an integration test. AFAIK this would mean spinning up an emulator to test on and then running tests that would actually create and modify a database on the emulator. This method sounds attractive because I would be able to do a 'round trip' test where I would start with some preconstructed data objects and could test all of the CRUD operations using them and a real db. This would allow me to actually verify that if I inserted an object and then read it back out, that the POJO that went in is the same that came out. I could also verify things such as ordering of a collection of objects, confirm that deletions really took place, database upgrades, etc...

A different method of testing that I could envision would involve using unit tests instead of integration tests. I could envision these tests involving verifying that a ContentValues object was created correctly or ensuring that given a certain Cursor object that a POJO was created correctly.

To me it seems like the integration test method is superior because it would provide good test coverage that the CRUD code that I've written is correct so that I won't run into situations where I expected a field of an object to be populated but it was null, or something like that.

What do other people do to test their Sqlite code on Android?

Can anyone help me understand the merits of unit tests vs. integration tests when it comes to Sqlite on Android?

1 Answer 1

2

Because of SQLite being implemented in Android, local unit test won't work for most of the code connected to it. Therefore you need to run an instrumented unit test for it. But this doesn't mean you need to write real integration tests.

You could use the context of the Instrumentation itself to create the databases you're testing. For this and some JUnit4 convenience define

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

in your gradle file and use InstrumentationRegistry.getContext() to access the context.

This has the advantage, that it also works for library tests not instrumenting any real application or just doesn't leave files in the context of it.

As of Android JUnit4 Testing - Where to get Context from? you could also use

new RenamingDelegatingContext(InstrumentationRegistry.getTargetContext(), "test_");

and test the databases in the context of your own application.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.