Você não está cansado de definir manualmente a classe AbstractModule do Guice apenas para fazer o RoboGuice injetar seus mocks em vez de implementações reais?
Em caso afirmativo, tente este executor JUnit4 :
MocksInjectingTestRunner.java
public class MocksInjectingTestRunner extends RobolectricRoboTestRunner {
public MyOwnTestRunner(Class<?> testClass) throws InitializationError {
super(testClass);
}
@Override
public void prepareTest(Object test) {
super.prepareTest(test);
MockitoAnnotations.initMocks(test);
try {
setupRoboguice(test, getListOfMocks(test));
} catch (IllegalAccessException e) {
throw new RuntimeException("Failed to get instance of object", e);
}
}
private List<Object> getListOfMocks(Object test) throws IllegalAccessException {
final Field[] declaredFields = test.getClass().getDeclaredFields();
List<Object> objects = new ArrayList<Object>();
for (Field field : declaredFields) {
if (field.getAnnotation(Mock.class) != null) {
field.setAccessible(true);
objects.add(field.get(test));
}
}
return objects;
}
private void setupRoboguice(Object test, List<Object> objects) {
RoboGuice.setBaseApplicationInjector(Robolectric.application, RoboGuice.DEFAULT_STAGE,
Modules.override(RoboGuice.newDefaultRoboModule(Robolectric.application))
.with(new MyRoboModule(objects)));
RoboGuice.injectMembers(Robolectric.application, test);
}
public static class MyRoboModule extends AbstractModule {
private List<Object> mocksToInject;
public MyRoboModule(List<Object> mocksToInject) {
this.mocksToInject = mocksToInject;
}
@Override
protected void configure() {
for (final Object mock : mocksToInject) {
Class clazz = mock.getClass();
bind(clazz.getSuperclass()).toInstance(mock);
}
}
}
}
Exemplo de uso:
Registrar.java
public class Registrar {
@Inject
private HttpConnection httpConnection;
public void register(User user) {
// stuff
}
}
HttpConnection.java
public class HttpConnection {
public Result execute(Request httpRequest) {
// stuff
return null;
}
}
E, finalmente, nossa classe de teste:
RegistrarTest.java
@RunWith(MocksInjectingTestRunner.class)
public class RegistrarTest {
@Mock
private HttpConnection httpConnection;
@Inject
private Registrar registrar;
@Test
public void mockShouldBeNotNull() {
assertNotNull(httpConnection);
}
@Test
public void shouldInjectMock() {
assertEquals(httpConnection, registrar.getHttpConnection());
}
}
Aproveitar!