1. Download the #1 Android News App:   EarlyBird - News for Android
    Dismiss Notice

Contexts in a JUnit test?

Last Updated:

  1. I've got a class that interfaces with the preferences that I'd like to unit test, but I can't figure out a way to pass it a context it can use for getSharedPreferences. Is there an easy (or even not-so-easy) way to do so?


  2. bwin

    bwin New Member

    Just in case you haven't already found this out for yourself in the past two weeks, I suggest you have a look at My life with Android :): JUnit in Android for an excellent article on JUnit and Android complete with a good example. To answer your question specifically, if your test cases inherit from android.test.AndroidTestCase instead of junit.framework.TestCase, you can call getContext() to get an Android Context.
  3. Thanks! I've been using the Activity Test Case with a dummy activity, but this way is much cleaner.

Share This Page