Android: how to get Context when testing with ActivityInstrumentationTestCase2? - android

I'm running a junit test in Android which extends ActivityInstrumentationTestCase2. I'm using this on order to start up an activity.
The activity uses a subclass of the application object to obtain some parameters. I get the application object from the context.
Unfortunately, ActivityInstrumentationTestCase2 does not provide access to the context. Is there a way to access the context before getting the activity?

You can get the application context from the instrumentation object:
getInstrumentation().getTargetContext().getApplicationContext()

To be able to inject an Application using setApplication() you should use ActivityUnitTestCase, as it is only available in this test case class.
By default, ActivityUnitTestCase, creates a hidden MockApplication object that is used as the application under test.

For those using
AndroidTestCase
and needing the app application subclass:
MyApplication context = (MyApplication) getContext().getApplicationContext();

Related

Application instance is not getting initialized with Robolectric and powermock

I have an activity which uses application instance for app level book keepings. I am saving app instance in static member after its creation(in application subclass itself). It works fine if I run with only Robolectric, but if I use Powermockito with Robolectric the application is not getting created and member varialble is null when I try to access it from activity.
Try to add your application class to the PowerMockIgnore annotation list.
E.g.
#PowerMockIgnore({ "org.mockito.*", "org.robolectric.*", "android.*","your_application"})
Hope it helps.

Why Android's AndroidTestCase class returns getcontext as null

I am using AndroidTestCase class where i need to get context. (I am using parent project for which i am writing testcase).
I have tried with getting context from mocktext and setting it through setup.also tried by using internal getTestContext() but nothing is working.
If any one is having idea then please let me know.
Kind Regards,
Sog
getContext of AndroidTestCase will return the context you've set with setContext.
if you want to mock a context you probably want to extend MockContext or use some mock library and pass that mock to the class under test.
If you're trying to test an activity of a service you'd probably want to use ActivityUnitTestCase or SerivceTestCase. then you might need to to use setContext, setActivityContext or setApplication to mock the context or application of the activity or service under test.

Application initialization

I am currently making an application which only has three classes. Two Activities, and an Application class. From what I have learned of Applications so far, the class initializes itself at the start of the program, so does that mean I do not need to initialize an Object of the class in each Activity?
My program crashes at the start every time and is returning a ClassCastException, which I'm assuming has to do with my Application class since it is the only class casting I am doing in all of my code. As a local variable I have
protected BluetoothApplication myBt;
and inside my onCreate() method I call
myBt = (BluetoothApplication)getApplication();
No you don't need to initialise it manually but you can use getApplicationContext() to get the instance of your Application Class for eg:-
MyApplication application = ((MyApplication)getApplicationContext());
You can also access Application class from a Non-Activity class by passing a Context to that class and then using that context for getting the instance of Application class by,
MyApplication application = ((MyApplication)context.getApplicationContext());
Is the BluetoothApplication a custom subclass of Android's default Application-class? If so, then do you tell Android in your AndroidManifest.xml to use that class instead of the default Application class?
See Android Application API for more details.

Calling an activity from an AndroidTestCase

I am writing an android test case which requires the execution of a seperate Activity to the Activity being tested (not for the sake of testing but just to gain access to the contentresolver so I can change some telephony settings).
Is it at all possible to start an activity from a test case or in another manner.
I am aware of the AndroidTestCase class used to test activities, an I am using it in my tests, however I need to use a ContentResolver to change telephony settings and then test the reaction of the activity under test so I need another application component to change these settings.
Note: I release the complexity behind multiple activity testing (requiring an ActivityManager) but I only want to use it's method to alter the settings so I could even have the logic in the onCreate method.
Android provides a special instrumentation framework for testing Activities. You must use this framework since Activities have a complex lifecycle that is un-invokable outside this provided framework. Look under the Testing link in the Developmentsection of the Android documentation for Activity Testing. If this doesn't answer your question, you might rephrase it a bit.
Edit
You should really be extending ActivityUnitTestCase to test an Activity, not AndroidTestCase. You get more functionality specific to what you need to test. If you extend ActivityUnitTestCase there is a function called launchActivity. It'll launch the activity you need and give you an instance of the activity so that you can call methods on it such as set, get, and finish. This should do anything you need for manipulating single and multiple activities at a time.
Example code:
#MediumTest
public class Test extends ActivityUniTestCase<HelloActivity> {
public Test(Class<HelloActivity> activityClass) {
super(activityClass);
}
#MediumTest
public void testLifeCycleCreate() {
HelloActivity hActivity = startActivity(new Intent(Intent.ACTION_MAIN), null, null);
getInstrumentation().callActivityOnStart(hActivity);
getInstrumentation().callActivityOnResume(hActivity);
GoodByeActivity gActivity = launchActivity("package.goodbye", GoodByeActivity.class, null);
gActivity.finish();
}
}
AndroidTestCase and ActivityInstrumentationTestCase2 both provide methods to get Context
AndroidTestCase:
getContext();
ActivityInstrumentationTestCase2
getInstrumentation().getContext();
You can use these contexts to launch another activity, however the permissions is adopted from the application under test, so in my case with the contentresolver I only have the same permission to alter settings I do in the application under test.
In my case this is no good so I had to create a seperate application with it's own permissions and a background service I was then able to control by launching intents using the context.

Where to get Context from standalone native application?

A standalone native application should perform calls to Java VM. Creating VM and playing with classes seem to be OK, but there is a problem while obtaining instance of Context. I can not create Application object and use it (NullPointerException is thrown). Also, Context class is abstract and can not be instantiated.
So, where can I get the Context object?.
Thanx.
Activity extends Context, so your Activity IS a Context.

Categories

Resources