Where is log output written when using Robolectric + Roboguice?

I use Robolectric to test Android. I run my tests through maven, for example.

mvn -Dtest=LogTest test 

If I have code that writes to logs, for example

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

or using Roboguice Ln

 Ln.d("blah"); 

I do not see output in maven surefire logs (text files).

Ideally, in fact, I want simple log instructions to come to the console. I can write to the console using System.out.println("blah") , but of course I would prefer to use the supported logging APIs.

So my question is: why don't I see the logs output at all and how can I get the log messages written to the console?

+43
java android maven-2 robolectric roboguice
Apr 19 2018-12-12T00:
source share
6 answers

I am running robolectric-2.0-alpha-3 .

For me, setting the setUp method of my stream test to stdout worked

Something like:

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

With this version of robolectric, I did not manage to do the same ( ShadowLog.stream = System.out ) in a custom TestRunner or in my TestLifeycleApplication.

Setting the system property System.setProperty("robolectric.logging","stdout"); also had no effect, but may work in previous versions.

+62
May 4 '13 at 16:09
source share

Im using robolectric 2.3. How it works for me:

In my @Before:

 ShadowLog.stream = System.out; 

Inside my test functions I can use (ShadowLog. There are other options):

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

And inside my tested class, I can put some messages in the log with:

 System.out.println("message"); 
+14
Oct 10 '14 at 13:20
source share

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

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

+11
Apr 20 2018-12-12T00:
source share

Add the following parameters to the test setup:

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

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

+5
Apr 12 '13 at 5:40
source share

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 tests locally, for example in intellij, then all you need is an environment variable: Just go (for intellij) to Run / Debug Configurations β†’ Defaults β†’ Junit β†’ VM Parameters and add

 -Drobolectric.logging=stdout 
+1
Dec 11 '14 at 0:18
source share

The solution that was best for me (or in general) was to initialize the replaced embedded implementation (only when testing) of the RoboGuice Ln.Print class to do System.out printing instead of printing Android magazine, since I was actually using Robolectric, so as not 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()); } } 

Of course, assuming the standard Robolectric init will connect the module to 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); } 
0
Jan 21 '13 at 19:35
source share



All Articles