1. Check out our companion app, Forums for Android! Download from Google Play

Contexts in a JUnit test?

Discussion in 'Application Development' started by James McMurray, May 5, 2009.

  1. James McMurray

    Thread Starter
    15

    May 5, 2009
    18
    0
    15
    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
    5

    May 18, 2009
    1
    0
    5
    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

    Thread Starter
    15

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

Share This Page

Loading...