Application instance is not getting initialized with Robolectric and powermock - android

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.

Related

How to use Okhttpclient object between activites using dagger 2

I have created a EmailComponent interface which has 2 modules EmailModule and Networkmodule , I am trying to share Email object and Okhttpclient object between activites . In ActivityA I did:
EmailComponent component = DaggerEmailComponent.builder()
.emailModule(new EmailModule())
.netWorkModule(new NetWorkModule()).build();
component.Email().sendEmail();
This is working fine. My question is how can I inject them into ActivityB Without calling again DaggerEmailComponent build code?
If i try using field injection in ActivityB like this code below it crashes.
#Inject Email email
email.sendMail(); // App crash.
Is what am trying to accomplish possible? If Yes, what am I doing wrong?
To avoid making the EamilComponent again and again you may make a Base Activity and move the code of making the component there. Or you may make a subclass of Application and make a static component there which can then be used from anywhere in the app. You must be getting NPE as the field may not have been initialized.
For using static component from the Application subclass create a method in your dagger component with your base activity as a parameter and then call that method using the static EamilComponent in Application subclass just after the onCreate of the activity. You can even call the method in the component before onCreate.

Why can't I call the method declared directly in the custom Application

It is a MVVM sample code from florina-muntenescu.
In his DroidconApplication.java, a getViewModel() method declared and called in the MainActivity.
Therefore I want to implement it into my project, I did the same thing declared in a custom Application, implemented the application into the AndroidManifest.xml, but couldn't find the getViewModel() in my MainActivity.
I searched for the answer, which is get the custom application first by
Application app = (CustomApplication) getApplication();
and call method by the app reference.
Then why not the same as the sample code?
what did I wrong?
Sample source code in Github
You have tried with this
Application app = (CustomApplication) getApplication();
app.getViewModel();
app.getViewModel() won't work because getViewModel() actually exist in CustomApplication class. It will work at runtime as app is an instance of CustomApplication but at compile time there is no way Compiler will understand that app is actually instance of CustomApplication.
So use this :
CustomApplication app = (CustomApplication) getApplication();
app.getViewModel();

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.

Android: how to get Context when testing with ActivityInstrumentationTestCase2?

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();

Categories

Resources