• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Contexts in a JUnit test?

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!
 
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.
 
Back
Top Bottom