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 { ... @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); }
Roberto Andrade Jan 21 '13 at 19:35 2013-01-21 19:35
source share