Is there a way to print out Logcat (Log.i, Log.d) messages when running a JUnit (method) test in Android Studio?
I can see System.out.print message but no logcat printouts.
In the runconfiguration (GUI window of Android Studio) there are logcat options for tests under Android tests but not for JUnit tests.
Is this possible somehow? Thanks for any hints!
Logcat output will not be seen in unit tests because Logcat is Android feature - JUnit tests can use only standard Java, therefore Android features won't work.
What you can do in unit tests is inject "test doubles" into tested components. But Log.x
calls are static, so you can't override them (without resolving to e.g. PowerMock, which you should avoid at all costs).
Therefore, the first step will be to introduce a non-static class that will behave as a proxy for Log.x
calls:
/**
* This class is a non-static logger
*/
public class Logger {
public void e(String tag, String message) {
Log.e(tag, message);
}
public void w(String tag, String message) {
Log.w(tag, message);
}
public void v(String tag, String message) {
Log.v(tag, message);
}
public void d(String tag, String message) {
Log.d(tag, message);
}
}
Use this class in every place you have Log.x
calls now.
Second step would be to write a test-double implementation of Logger
that redirects to standard output:
public class UnitTestLogger extends Logger{
@Override
public void e(String tag, String message) {
System.out.println("E " + tag + ": " + message);
}
// similar for other methods
}
The last step is to inject UnitTestLogger
instead of Logger
in unit tests:
@RunWith(MockitoJUnitRunner.class)
public class SomeClassTest {
private Logger mLogger = new UnitTestLogger();
private SomeClass SUT;
@Before
public void setup() throws Exception {
SUT = new SomeClass(/* other dependencies here */ mLogger);
}
}
If you want to be rigorously strict about OOP concepts, you can extract common interface for Logger
and UnitTestLogger
.
That said, I never encountered a need to investigate Log.x
calls in unit tests. I suspect that you don't need it either. You can run unit tests in debug mode and step over the code line-by-line in debugger, which is much faster than attempting to investigate logcat output...
General advice:
If the code you are testing contains Log.x
static calls and your unit tests do not crash - you have a problem.
I'd guess that either all tests are being run with Robolectric
, or you have this statement in build.gradle: unitTests.returnDefaultValues = true
.
If you run all the tests with Robolectric
, then it is just unefficient, but if all Android calls return default values, then you test suite is not reliable. I suggest you fix this issue before proceeding any further because it will bite you in the future one way or another.
I was searching for this same thing and didn't ever find a straight answer. I know this question is over a year old but still, it'd be good to have an answer here for future reference.
The android.util.Log class logs directly to Logcat and the implementation for android.util.Log is not available when running the unit tests on the local JVM. You'll receive an error when trying to use the Log class in your unit tests because "the android.jar file used to run unit tests does not contain any actual code (those APIs are provided only by the Android system image on a device)."
See Android Documentation on Unit Testing
So if you really want to use android.util.Log you'll need to mock it locally and utilize System.out.print to print to the console. To start, add PowerMockito to your project. If you're using Gradle, you can just add the following dependencies:
testCompile 'junit:junit:4.12'
testCompile 'org.powermock:powermock:1.6.5'
testCompile 'org.powermock:powermock-module-junit4:1.6.5'
testCompile 'org.powermock:powermock-api-mockito:1.6.5'
Next I used Steve's answer here to figure out how to return a parameter passed into a mock object using Mockito.
The result was something like:
import android.util.Log;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import static org.mockito.Matchers.anyString;
import static org.powermock.api.mockito.PowerMockito.when;
@RunWith(PowerMockRunner.class)
@PrepareForTest({Log.class})
public class SomeUnitTest {
@Test
public void testSomething() {
System.out.println("Running test");
PowerMockito.mockStatic(Log.class);
// Log warnings to the console
when(Log.w(anyString(), anyString())).thenAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
Object[] args = invocation.getArguments();
if (args.length > 1) { //cause I'm paranoid
System.out.println("Tag:" + args[0] + " Msg: " + args[1]);
}
return null;
}
});
Log.w("My Tag", "This is a warning");
}
}
Hope this helps someone!