I have an Activity
I am unit testing. I want to test the Activity
in isolation, so the ActivityUnitTestCase fits my purposes very well.
The onCreate
method of my Activity
uses a value that is stored in the Preferences. I would like to set a value for this in my test case.
The problem is that I can't figure out how to set the Preference
ahead of time. There is not a getContext()
method in ActivityUnitTestCase
, and getActivity()
, which should return a context
, is null until I call startActivity()
. This is not an option because calling startActivity
will trigger the onCreate
method, and this is where the preferences code lives.
Any thoughts on how I can get a context
that I can use to manipulate the preferences for my unit tests?
You can get the context via the instrumentation.
To get the Context of instrumentation (test runner):
getInstrumentation().getContext()
But you probably need the context of the instrumented application:
getInstrumentation().getTargetContext()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With