How to get the context of the project which is under test.I have a class which is singleton,To create object for this i need to call one constructor of this class in setUp() method of testcase class,but i need to pass some context as a parameter to call that constructor, but i dont know how to get the context of this testcase class.I got confused whether i shuld pass the context of testcase class or the class which is under test.could any one help in this please.Could any one help me in this
you can get getApplicationContext() or getBaseContext or simply this!
MockContext wouldn't be a good choice, since there are no methods implemented. Every method call will give you an exception.
There is an IsolatedContext which has some stub methods implemented, but still throws a few exceptions.
If your class is extending AndroidTestCase you can simply call getContext() to receive an instance of Context.
Never used it myself, but the answer is probably here :
http://developer.android.com/reference/android/test/mock/MockContext.html
Related
Can't use ArrayAdapter in a custom dialog.
This is the error I am getting
You're getting that error because the ArrayAdapter constructor requires the first argument to be a Context object. Since you're calling the constructor from inside an OnClickListener, this refers to the listener, not your activity (i.e. not a Context).
You can qualify the this keyword with the name of your outer class. Assuming that code is written inside of MainActivity, you could write MainActivity.this instead.
Using this won't work since you are in the scope of the OnClickListener.
You should try getContext() or getApplicationContext() instead of this.
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.
How do I call the functions provided by Activity class from a class that does not extend Activity? Theoretically, yes, if I don't extend Activity I cannot directly use the functions provided by it. But is there a workaround provided for this? If not, are there replacements or alternative ways for these functions?
For example,
If my class extends Activity, I can call setContentView() to instantiate my layout xml file. But if my class extends some other class and doesn't extend Activity, then I can use the LayoutInflater to do the task. But what about other functions like registerReceiver() ? How do I get the functionality of 'registerReceiver()' from any other class , obviously I wouldn't want every such class to extend Activity. Static access by "Activity.function_name" is also not possible as these functions are not static.
Certain services can be accessed from anywhere. For example 'println()' or Log.e(),System functions can be called from anywhere, whenever needed. Is there a similar way for other critical functions?
Conclusion:
Pass Context to destination class. For accessing some functions however, type-casting the passed Context to Activity is required.
Both Changdeo's and BT's answers are correct.
Thanks.
Although I have not found any documentation explicitly stating why, in every case where I have ever needed to do this, simply passing the Activity's Context is sufficient.
For a Context called contextActivity passed into any function, the following will allow access to these member functions you require:
((Activity) contextActivity).<anyMemberFunction>
Or if you need these functions in multiple cases it might be simplest just to do the following:
Activity myActivity = (Activity) contextActivity;
From there you can access these Activity member functions whenever you like by using:
myActivity.<desiredFunction>;
As I mentioned, I have never found any case where this hasn't worked, but also no solid documentation saying this will always work. This is the trick I have seen consistently used though. If anyone has more to add, please do.
For Ex
Class XYZActivity extends Activity
{
......
......
MyClass myClass = new MyClass(this);
// OR you can pass just context
// MyClass myClass = new MyClass(getContext());
}
Class MyClass
{
Context context;
Myclass(Context context)
{
this.context = context;
context.registe....//Or any function
}
}
I have a helper class that I need context so I can access the SharedPrefences. Other posts recommend passing in the application context on instantiation of the helper class. So I made that change, it works very well except within a tab activity. The tab activity need to call a webservice to determine what data to display. The helper class makes the webservice call.
You can call getContext() from any activity. If the helper class is defined as a subclass of the activity, it can call it directly. Otherwise, passing the context through instantiation would be my second choice. I agree, it's not pretty passing contexts everywhere. There are probably some complicated OOP patterns you could use to avoid this, but I can't see it being an advantage overall.
If you get a null pointer you might be calling the function too early. In what function are you calling it?
I have created an ExpandableListAdapter class and I need to send it the context from the activity that is accessing it.
MyActivity.class:
MenuExpandableListAdapter.useInstanceContext(getApplicationContext());
MyExpandableListAdapter.class:
static Context context;
public static void useInstanceContext(Context applicationContext) {
context = applicationContext;
}
The above code works, but this also works:
MenuExpandableListAdapter.useInstanceContext(this.getApplicationContext());
What's the difference? Is this a good way to pass context? I'm still trying to fully understand context.
Context is necessary in order to get access to the resources and some other things. So, both - application and activity contexts work. But a good practice is tight to the smallest thing, which works, which is activity in your case. So, I would suggest new way for you:
MenuExpandableListAdapter.useInstanceContext(this);
Also, in your example, there is no difference between the calls. this is just the reference to the current object.
this refers to the object that is currently executing code, if the method is declared in the same class, and is not static, it is the same to call:
getApplicationContext()
and
this.getApplicationContext()
(The same applies to class members)
MenuExpandableListAdapter.useInstanceContext(getApplicationContext());
The getApplicationContext() method will called by using calling/current object implicitly.
in second you are giving calling/current object explicitly because this is special object that is always refer to calling object.
i suggest you to use this
MenuExpandableListAdapter.useInstanceContext(this.getApplicationContext());
because as per my reading it's good practice.