Contexts in a JUnit test?


Last Updated:

  1. James McMurray

    James McMurray Member This Topic's Starter

    Joined:
    May 5, 2009
    Messages:
    18
    Likes Received:
    0
    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?

    Thanks!
     

    Advertisement
  2. bwin

    bwin New Member

    Joined:
    May 18, 2009
    Messages:
    1
    Likes Received:
    0
    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.
    HTH
    Brian.
     
  3. James McMurray

    James McMurray Member This Topic's Starter

    Joined:
    May 5, 2009
    Messages:
    18
    Likes Received:
    0
    Thanks! I've been using the Activity Test Case with a dummy activity, but this way is much cleaner.
     

Share This Page

Loading...