Isolating units and writing tests in Android
My project has the run-of-the-mill HTTP calls to fetch XML files, parsing the XML files, and creating domain objects. Those objects are later used in the actions and services of the Android app.
I'd like to isolate that code. I also want to write tests for it. What are some good ways of doing this?
Creating an Android Library doesn't seem appropriate. The tests would have to be in a separate project. And ther开发者_JAVA技巧e are no services or activities in the extracted HTTP and XML related code. So the test suite doesn't have to be an Android test suite, it could be regular JUnit tests.
Note: I'm new to both Java and Android.
From my experience, the way I isolate things is by breaking in up into smaller methods - something that I learnt from http://www.infoq.com/news/2009/04/10-Ways-to-Better-Code-Neal-Ford and there is also presentation. Then you test against those specific methods in a normal JUnit TestCase. If you find certain methods are not testable, I find just breaking them even further down into smaller methods really helps.
I'd make a second pure Java library (not an Android library). Put all the non-Android code in there, like the code to parse the XML. Your Android code would depend on the library. Then, write tests for the library. These can be standard JUnit tests, maybe with some mocking. Once you have the library tested, see how much code you can move into the library. The Android-specific code should be as small as possible.
For testing the Android-specific code, I don't know. I've not gotten that to work.
精彩评论