Posts Tagged ‘mocking’

Testing in Android with mock UI components

1 Comment »

As described in an earlier post, Android is not friendly to mocking frameworks or mock-style testing.  If you want to test any class in your application that deals with the Android API, it’s best to run your tests through the emulator, accessing real Android classes.  It’s unfortunate because you’re not just testing your application.  You’re also testing Android.  Anyway, here’s a way to mock a UI component.

If you’re just starting, here are a couple notes to keep you on the right track.

  • Android is bundled with JUnit 3.  Don’t try using an updated JUnit library or another testing framework.  The Android jar doesn’t contain any functional code, so all test cases have to be run in the emulator, which uses JUnit 3.  The test framework that will work best is in the Android API.
  • If you need basic implementations of Android classes, try to avoid mocking them.

“Mock”ing

In my latest test, I needed a TextView so that I could call the simplest method, setText(String).  I’ll describe how I got one.

Don’t bother with the android.test.mock package.  It just contains implementations of classes that throw UnsupportedOperationExceptions.  There isn’t anything there that I have yet found useful.

  1. In the test case, instead of extending TestCase, extend at InstrumentationTestCase or, if necessary, one of its subclasses.  It’ll set up most of the stuff that’s available in an Activity and make it available to your test case.
    public class AnAndroidTest extends InstrumentationTestCase {
  2. Create a mock implementation of TextView or the class you need.
    public class MockTextView extends TextView {
      public MockTextView(final Context context) {
        super(context);
      }
    }
  3. The TextView constructor needs a real Context object because it will call methods on it.  The context is difficult to mock because parts of it are private to Android.  Since the Android JAR is just an API and doesn’t have any functional code, you couldn’t even see the methods if you tried.  They only exist in the VM in the emulator.  AFAIK, if you can’t see a method, you can’t mock it.  That’s why your test case extends InstrumentationTestCase.  Put this in it.
    final TextView textView = new MockTextView(this.getInstrumentation().getContext());

Now write the test case.  The text view is real and has a fully functional context, so the emulator will have everything it needs to support your test case.


Mocking classes in Android with JMock, JMockIt or EasyMock

5 Comments »

* I stand corrected.  A commenter, Kiran Gunda, says it’s possible to mock f inal methods with JMockIt.  I may have set the mock up incorrectly.  If I try again, I’ll update this post.  Until then, an updated post describes how I used Android test classes to run my code.  Matt also pointed out that JMock can mock final methods using JDave’s unfinalizer.

* Updated July 11, 2009 to remove some incorrect information

So that you don’t have to read this whole post, I’ll sumarize what I discovered.  If you’re thinking about using a mocking library to mock Android classes, don’t.  Android uses interfaces sparsely.  That makes a clean API with lots of reusable code, but makes mocking difficult.  The real problem, though, is caused by Android’s generous use of final methods.  I don’t know of any mocking library that can mock final methods, so you just can’t do it.  Instead of writing mocks, look into the android.test and android.test.mock packages.

I developed GreenMileage so that I could learn about the Android OS.  Now that I have an understanding of it, I decided to go back and write test cases so that I could continue development using TDD.  I started by writing tests for utility classes.  That wasn’t a problem.  After finishing that, I moved to some simple UI cases.  The first was TextViewCallbackListener.  I just had to mock TextView.setText(String).

First, I spent hours just trying to get the JMock jars into the project.  For some reason, the compiler would not put JMock into the APK.  I didn’t get any error message or notification.  It just wasn’t there.

I decided to use Android’s test mocks to run my code.  Click here to see a simple example.