When I first setup my application I wanted to store some data in the application class. I think I am doing this in a "safe" way, but that is not really the subject of this question.
Here is an example of what I have now.
private static String email;
public static void saveEmailId(String emailToSave) {
cachedPersistData.saveEmailId(emailToSave); // save to file
email = emailToSave;
}
public static String getEmail() {
return email;
}
public void onCreate() {
...
email = persistData.readEmailId(); // restore from file
...
}
The idea being if I always used the static methods, I would be setting and getting the value s from the same place. (I did not really understand singletons at the time , and read some blogs that implied that was not a good way to go anyway).
This leaves me with a lot of calls like this in my application.
MyApp.saveEmailId("blah");
MyApp.getEmailId();
Now that I am learning about testing (using Robolectric and Mockito) I think those static method calls are going to be problematic. First off, am I wrong about that? I saw something called PowerMock which might help with this?
I have started leaning about and using Dagger, so I am considering refactoring these methods in to non-static methods and using an @Singleton annotated Dagger provider to inject an instance of the app object whenever I need to make a call to one of these methods.
Since I think the refactoring might me a big job, would like some help trying to understand the pro's and con's of
- Leaving the code as is (static variables and methods) and trying to work around the test implications.
- Refactoring the static methods and calls into calls on a Dagger injected singleton instance.
UPDATE
I am beginning to wonder if I can eliminate the static method calls
In order to inject an instance of the application into a class I need the ObjectGraph which is in the application.
so if I have an annotated field like this
@Inject Myapp app;
In a class constructor or onCreate() method I think I would still need something like
public void onCreate() {
MyApp.inject(this);
}
This seems like a chicken and the egg problem.