1. 2015 is going to be a great year for Android! Why wait??
    Samsung Galaxy Note 5 | Samsung Galaxy S6 | HTC One M9
  2. New Forum Created: Samsung Gear VR!

Contexts in a JUnit test?

  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