[ACCEPTED]-Where is log output written to when using Robolectric + Roboguice?-robolectric

Accepted answer
Score: 79

I am running robolectric-2.0-alpha-3.

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

Something like:

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

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

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

Score: 14

Im using robolectric 2.3. How works for 4 me:

Into my @Before:

ShadowLog.stream = System.out;

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

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

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

System.out.println("message");
Score: 12

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

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

Score: 5

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

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

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

Score: 2

When running tests with maven all you need 6 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 5 tests locally, e.g. in intellij, then all 4 you need is an environmental variable: Just 3 go (for intellij) to Run/Debug Configurations 2 --> Defaults -->Junit --> VM options and 1 add

-Drobolectric.logging=stdout
Score: 0

The solution that worked out best for me 10 (or at all) was to initialize a replacement 9 injected implementation (during testing 8 only) of RoboGuice's Ln.Print class to do 7 System.out printing instead of Android's 6 Log printing, given I was actually using 5 Robolectric to avoid having to depend on 4 the Android subsystem to run my tests in 3 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 2 of course assuming the standard Robolectric 1 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);

}

More Related questions