I need to run unit tests for code that references SQLiteDatabase for my Android code; however all my attempts to instantiate this object outside the emulator (on my desktop machine) have failed. JDBC on Android is not being recommended on the Net, hence it's out of the question (I could have provided mock objects very easily that way). So, any ideas?
-
It is very unclear what you are trying to do. On the one hand, you talk about unit testing Android code, but then talk about your desktop machine. I do not know how those two problems relate. – CommonsWare Feb 22 '10 at 13:50
-
sending the code to the emulator for testing takes too long. i would like to be able create, populate a db through JUnit or TestNG, without having to send the code to the emulator. however i am not able to use Android sqlite objects outside the emulator. – burak bayramli Feb 22 '10 at 14:54
-
If all you are trying to do is manipulate a SQLite database outside of Android on your desktop, there are many options -- http://stackoverflow.com/questions/41233/java-and-sqlite – CommonsWare Feb 22 '10 at 15:22
-
The catch is: the same code needs to work both inside (the emulator) and outside Android. That is, I need to be able to execute DB code from android.database package, both on emulator and through JUnit/TestNG on desktop. Will the code you shared let me do that? – burak bayramli Feb 22 '10 at 15:37
-
I guess my question in simplest terms is this: How can I open a database using android.database classes inside a main()? – burak bayramli Feb 22 '10 at 16:08
2 Answers
You cannot test android classes outside the emulator or a real device. If you want to run test cases on your desktop computer, using JVM instead of Dalvik, they should be independent of android classes. You may also change android.jar not to throw exceptions, but I guess this is not what you want.

- 915
- 1
- 12
- 20

- 65,697
- 9
- 111
- 134
-
I believe the best way forward is mocking android.db classes with JDBC - a bit weird but using JDBC *on the phone* is not recommended. This method would have to use different classpath for testing so classes using a desktop sqlite database come before android classes. I believe this could work. db.execSQL calls will map to statement.execute() calls one-to-one; Cursor delegation could be little tricky but manageable. In any case tho, some form of testing *must* be used for android.db calls. Sending code repeatedly to emulator for testing simply sucks. – burak bayramli Feb 22 '10 at 23:19
-
1Actually, you can test android classes outside the emulator/device, and you can do it on the JVM! I use http://pivotal.github.com/robolectric/ to defang the Android SDK, then I use Mockito to mock out the stuff I'm not interested in in my tests. – Christopher Perry Jan 02 '12 at 08:43
Hit the same issue and I agree with Burak. Did you make any progress with mocking up the android.db classes? You could also use sqlite4java on desktop but the issue is that it's difficult to provide a facade for the existing Android's SQLlite mock-up wrappers.
update
Perhaps difficult, but not totally impossible. Since I couldn't find any existing projects that would allow me to test sqlite with the Android classes on my development machine, I went ahead and implemented Android's SQLiteOpenHelper and SQLiteDatabase (plus a few other minor classes) as wrappers for sqlite4java, good enough for my unit testing purposes. Ah, now the execution time for my android-sqlite specific unit tests are down .1 seconds.

- 2,157
- 1
- 22
- 21
-
2
-
I second Dan Dyer's plea: Please could you provide your source for doing this? Thanks. – mydoghasworms Jan 14 '13 at 06:32