I would like to log information in model classes - not necessarily for unit testing purposes but for real life scenarios where I am trying to debug.
However, if I try to use android.util.Log
methods I get the following errors when running JUnit tests:
java.lang.RuntimeException: Method d in android.util.Log not mocked. See http://g.co/androidstudio/not-mocked for details.
I understand why this occurs, I should not be using Android framework code in model classes that are designed to be framework independent! I'm not really arguing against the error, but rather I am trying to find a way to work around this.
I have one idea, does this make sense?
Create a CustomLog
class along these lines:
public class CustomLog {
private static ILogger mLogger;
public static void setLogger(ILogger logger) {
mLogger = logger;
}
public static void e(String tag, String message) {
mLogger.e(tag, message);
}
}
Where ILogger
is an interface with the required methods to perform the log functionality (e, d, etc. methods...)
I could create an ILoggerImpl
that uses the android.util.Log
methods, and a MockLogger
class that simply prints out to System.out.println
and/or does nothing (or anything else!).
I think that'd perfectly fit my needs (I would be required to setup my CustomLog
class very early on in the lifecycle, but that's not a huge deal).
However, if I ever needed to add third party libraries/outside code to my model classes, this would likely break again in the same manner if the new libraries/code use android.util.Log
methods.
So, is there a "catch all" type behavior I could use? What do you think?