Recently, I have been playing around with Android. Unit-testing your application on Android is slightly cumbersome: the class that you test cannot use any class from AndroidSDK, e.g. View or Activity.
As we will see in the example below, in many cases you can achieve a reasonable testability of your code.
We wish to create a simple game. The view should show playground in which there is several balls. For now we are interested in a snippet that would allow us moving those balls freely around playground.
To that end we create classes Playground and Ball. Playground serves as a facade for accessing Balls. Ball is an object that knows it's position, size and identity.
A naive implementation would look like this: As noted above such code is not unit-testable. A way to work around that is to separate the infrastracture classes (PlaygroundView) from model (PlaygroundModel) by a bridge (Hand). Now the implementation Hand is Android independent and we can finally test it :-)
No comments:
Post a Comment