Unit testing SQLiteDatabase code - how do I instantiate a DB instance outside the emulator
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?
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.
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.
精彩评论