Using Roboguice in Tests in Android Studio

587 Views Asked by At

So after spending a day or so trying to get robolectric to work with gradle using the android-gradle-plpugin I read that the creator thinks it's too much hassle and doesn't use it himself

So thats a big enough reason for me not to use it either. However now I cannot find any docs on how to set up normal android tests to run on the emulator. Its seems they all relate to eclipse. How do I get normal Android testings running with Android studio. I presume I need to modify build.gradle but how do I do that?

I would also like to use roboguice to inject my dependencies into the test cases.

EDIT

So I took a stab in the dark and I tried this but the test returns false (a fail)

public class SearchTest extends ActivityTestCase {

    @Inject
    private ObjectMapper objectMapper;

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        RoboInjector injector = RoboGuice.getInjector(getActivity());
        injector.injectMembersWithoutViews(this);
    }

    public void shouldSerialise() {
         System.out.println("called should serialise");
        Assert.assertNotNull(objectMapper);
    }

}

EDIT 2

So I have tried a different approach. I followed this tutorial which does seem to run the test however I am having a problem with providing a manifest find as I get the following error,

WARNING: No manifest file found at ./AndroidManifest.xml.Falling back to the Android OS resources only. To remove this warning, annotate your test class with @Config(manifest=Config.NONE).

I then used this test runner instead...

public class RobolectricGradleTestRunner extends RobolectricTestRunner {

    public RobolectricGradleTestRunner(Class<?> testClass) throws  org.junit.runners.model.InitializationError {
        super(testClass);
    }

    @Override protected AndroidManifest getAppManifest(Config config) {
        String manifestProperty = System.getProperty("android.manifest");
        if (config.manifest().equals(Config.DEFAULT) && manifestProperty != null) {
            String resProperty = System.getProperty("android.resources");
            String assetsProperty = System.getProperty("android.assets");

            return new AndroidManifest(Fs.fileFromPath(manifestProperty), Fs.fileFromPath(resProperty),
                    Fs.fileFromPath(assetsProperty));
        }
        return super.getAppManifest(config);
    }
}

With no luck. Would I be better reverting back to intellij and purely using maven?

0

There are 0 best solutions below