opensource.google.com

Menu

expect(easier testing).andReturn(Android Mock)

Wednesday, May 26, 2010

The Android development tools provide developers with a host of tools for creating fun, useful and compelling Android applications. Included in this tool-set are various testing tools that make it easier to ensure the quality of the applications. One tool that has been missing, however, is a mocking framework.

Objects Mocking is a common technique in the tester’s skillset. Mocks are simulated objects which mimic and take the place of real objects by replaying pre-recorded behavior. They are used to quickly build more focused tests that reflect better the behavior of the tested objects. This is achieved by breaking long object construction chains, isolating interesting behavior to tighten a test’s focus. “Mocking” various services such as a network connection, a database or even a per-use credit card charging service (the kind you don’t want to stress-test with your personal credit card number!) makes it possible to test real external interactions without ever touching the real thing.

Here at Google, we use mocking frameworks a lot when writing Java tests (for the reasons above, and more) but unfortunately, the solutions available only provide limited mocking capabilities for Android testing.

That is, until now:

Android Mock has been written as an extension of EasyMock to allow for the mocking of Java Interfaces and Classes on Android’s Dalvik VM. Check out the resources for how to use it and how to write tests, then get mocking.

.