Skip to content

Instantly share code, notes, and snippets.

@nathan815
Last active July 11, 2021 06:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nathan815/ce931e4f104e915d1dd43064b13ae91d to your computer and use it in GitHub Desktop.
Save nathan815/ce931e4f104e915d1dd43064b13ae91d to your computer and use it in GitHub Desktop.
android.util.Log class override for unit tests

This is an override of the android.util.Log class to allow testing code which calls Log methods.

Place these two classes in src/test/java/android/util.

To enable or disable logging to stdout just change the enablePrinting variable.

LogTestConfig.enablePrinting = true;
package android.util;
public class Log {
public static int v(String tag, String msg) {
print("VERBOSE", tag, msg);
return 0;
}
public static int d(String tag, String msg) {
print("DEBUG", tag, msg);
return 0;
}
public static int i(String tag, String msg) {
print("INFO", tag, msg);
return 0;
}
public static int w(String tag, String msg) {
print("WARN", tag, msg);
return 0;
}
public static int e(String tag, String msg) {
print("ERROR", tag, msg);
return 0;
}
private static void print(String type, String tag, String msg) {
if (LogTestConfig.enablePrinting) {
System.out.println(type + ": " + tag + ": " + msg);
}
}
}
package android.util;
public class LogTestConfig {
public static boolean enablePrinting = false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment