52

I'm using Robolectric to test Android. I'm running my tests via maven, e.g.

mvn -Dtest=LogTest test

If I have code that writes to the logs, such as

Log.d("TAG", "blah");

or using Roboguice's Ln

Ln.d("blah");

I don't see any output in maven's surefire logs (text files).

Ideally, I actually want simple log statements to go to the console. I can write to the console by using System.out.println("blah"), but of course I'd rather use the supported logging APIs.

So my question is, why am I not seeing log output at all, and how can I get the log messages written to the console?

Tyler Collier
  • 11,489
  • 9
  • 73
  • 80
  • Does it work with info / error? – artbristol Apr 19 '12 at 10:40
  • This is an old question, but I do want to ask Tyler to accept an answer. When you accept answers to your questions, people are more willing to answer your questions. – Christine Aug 07 '14 at 16:32
  • @Christine I agree Christine. However, I no longer have the ability to check if answers posted here work. At the time, none of the answers helped me. I upvoted scompt.com's answer, but then realized it didn't solve the issue for me, as I noted in the comments. Is there a policy/guide for what I should all this time later? – Tyler Collier Aug 07 '14 at 17:03
  • Jesus Monzon Legido's solution works fine, I use that. Also, it's the simplest solution given. – Christine Aug 08 '14 at 09:36

6 Answers6

79

I am running robolectric-2.0-alpha-3.

What worked for me was to set in the setUp method of my test the stream to stdout

Something like:

@Before
public void setUp() throws Exception {
  ShadowLog.stream = System.out;
  //you other setup here
}

With this version of robolectric I had no success doing the same (ShadowLog.stream = System.out) in a custom TestRunner or in my TestLifeycleApplication.

Setting the system property System.setProperty("robolectric.logging","stdout"); was of no effect as well, but it might works in previous versions.

Jesus Monzon Legido
  • 1,253
  • 12
  • 10
15

Im using robolectric 2.3. How works for me:

Into my @Before:

ShadowLog.stream = System.out;

Inside my test functions i can use (ShadowLog. have other options):

ShadowLog.v("tag", "message");

And inside my tested class I can put some messages at log with:

System.out.println("message");
Bamumi
  • 174
  • 2
  • 6
  • I've tried it, but I see nothing. Where is the output written? Btw.: I use Robolectric 2.4, but I think logging will work in the same way. –  Dec 03 '14 at 19:12
  • U can see the result in the logcat. Verify if u put the ShadowLog.stream = System.out; into ur setup. – Bamumi Dec 04 '14 at 15:38
  • Why not just use `System.out`? Why the necessity for `ShadowLog`. – IgorGanapolsky Apr 01 '16 at 16:57
13

By default, logging output when using the RobolectricTestRunner disappears. You can configure where it goes by looking at the setupLogging() method of that class.

To summarize, you need to set the robolectric.logging system property to either stdout, stderr, or a file path where the log should be written. I do this in the constructor of a subclass of RobolectricTestRunner that I use for all tests so that logs always get written to stdout.

Edward Dale
  • 29,597
  • 13
  • 90
  • 129
  • 1
    It worked! Thanks! Good links too. Now... did you read about that somewhere, or did you just dig through the source? Also, are you familiar with Roboguice? I was wanting to use their "Ln" logging, but if I do, it doesn't get printed out to stdout, although it's strange because it seems like its default implementation is via `android.util.Log`, which should work because I can use Log directly. Thoughts? – Tyler Collier Apr 21 '12 at 00:13
  • I only found it by slogging through the code. I don't think it's documented anywhere. As for `Ln`, make sure your app is `debuggable`, otherwise `Ln` [automatically disables debug and verbose logging](http://code.google.com/p/roboguice/wiki/Logging). – Edward Dale Apr 21 '12 at 17:34
  • Rats. Sorry to be dense. I realize this did not actually solve my problem. The code you pointed out makes sense, but it doesn't actually work. I thought it was working, but it was leftover code I had used to make it work, from: http://groups.google.com/group/robolectric/browse_thread/thread/f1a5df8b3849a4f3?pli=1 – Tyler Collier Apr 24 '12 at 22:51
  • 1
    Does setupLogging() still exist? http://pivotal.github.io/robolectric/javadoc/com/xtremelabs/robolectric/RobolectricTestRunner.html – emmby Apr 12 '13 at 05:37
  • Yes, it's still in [RobolectricTestRunner](https://github.com/pivotal/robolectric/blob/master/src/main/java/org/robolectric/RobolectricTestRunner.java#L695), but the package has been changed. – Edward Dale Apr 12 '13 at 07:53
5

Add the following to your test setup before your test runs:

ShadowLog.stream = System.out;
Robolectric.bindShadowClass(ShadowLog.class);

https://groups.google.com/forum/?fromgroups=#!msg/robolectric/PK-9cQQQROw/svuQzM5h_vsJ

emmby
  • 99,783
  • 65
  • 191
  • 249
2

When running tests with maven all you need is something like this :

                 <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.17</version>
                    <configuration>
                        <systemPropertyVariables>
                          <robolectric.logging>stdout</robolectric.logging>
                        </systemPropertyVariables>
                    </configuration>
                 </plugin>

When running the tests locally, e.g. in intellij, then all you need is an environmental variable: Just go (for intellij) to Run/Debug Configurations --> Defaults -->Junit --> VM options and add

-Drobolectric.logging=stdout
sakis kaliakoudas
  • 2,039
  • 4
  • 31
  • 51
0

The solution that worked out best for me (or at all) was to initialize a replacement injected implementation (during testing only) of RoboGuice's Ln.Print class to do System.out printing instead of Android's Log printing, given I was actually using Robolectric to avoid having to depend on the Android subsystem to run my tests in the first place.

From Ln.java:

public class Ln  {
...

/**
 * print is initially set to Print(), then replaced by guice during
 * static injection pass.  This allows overriding where the log message is delivered to.
 */
@Inject protected static Print print = new Print();

So basically:

public class TestModule extends AbstractModule {

    @Override
    protected void configure() {
        bind(Ln.Print.class).to(TestLogPrint.class);
    }

}

and:

public class TestLogPrint extends Print {

    public int println(int priority, String msg ) {

        System.out.println(
            String.format(
                "%s%s", 
                getScope(4), 
                msg
            )
        );

        return 0;
    }

    protected static String getScope(int skipDepth) {
        final StackTraceElement trace = Thread.currentThread().getStackTrace()[skipDepth];
        return String.format("%s | %s.%s | ", new Date(), trace.getFileName().replace(".java", ""), trace.getMethodName());
    }
}

That of course assuming the standard Robolectric init to hook the module up with RoboGuice:

@Before
public void setUp() throws Exception {

    Module roboGuiceModule = RoboGuice.newDefaultRoboModule(Robolectric.application);
    Module productionModule = Modules.override(roboGuiceModule).with(new CustomRoboModule());
    Module testModule = Modules.override(productionModule).with(new TestModule());

    RoboGuice.setBaseApplicationInjector(Robolectric.application, RoboGuice.DEFAULT_STAGE, testModule);
    RoboGuice.injectMembers(Robolectric.application, this);

}
Roberto Andrade
  • 1,793
  • 1
  • 21
  • 27