Android functional testing with Dagger

不羁岁月 提交于 2019-12-03 11:12:34

I use Mockito and Dagger for functional testing. The key concept is that your test class inherits from ActivityUnitTestCase, instead of ActivityInstrumentationTestCase2; the latter super-class call onStart() life-cycle method of Activity blocking you for inject your test doubles dependencies, but with first super-class you can handle the life-cycle more fine-grained.

You can see my working examples using dagger-1.0.0 and mockito for test Activities and Fragments in:

https://github.com/IIIRepublica/android-civicrm-test

The project under test is in:

https://github.com/IIIRepublica/android-civicrm

Hope this helps you

I did some more experimenting and found out that Dagger is not able to create activity correctly when it is injected to test. In the new version of test, testDoSomethingCalledOnEngine passes but onCreate is not called on the MainActivity. The second test, testDoSomethingUI fails and there are actually two instances of MainActivity, onCreate gets called to the other instance (created by ActivityInstrumentationTestCase2 I quess) but not to the other. Maybe the developers at Square only thought about testing Activites with Robolectric instead of Android instrumentation test?

public class MainActivityTest extends
    ActivityInstrumentationTestCase2<MainActivity> {

@Inject Engine engineMock;
@Inject MainActivity mActivity;

public MainActivityTest() {
    super(MainActivity.class);
}

@Override
protected void setUp() throws Exception {
    super.setUp();

    // Inject engineMock to test & Activity under test
    ObjectGraph.create(new TestModule()).inject(this);
}


 @Module(
 includes = MainModule.class,
 entryPoints = MainActivityTest.class,
 overrides = true
 )

static class TestModule {
    @Provides
    @Singleton
    Engine provideEngine() {
        return mock(Engine.class);
    }
}

public void testDoSomethingCalledOnEngine() {
    when(engineMock.isLoggedIn()).thenReturn(true);
    mActivity.onSomethingHappened();
    verify(engineMock).doSomething();
}

@UiThreadTest
public void testDoSomethingUI() {
    when(engineMock.isLoggedIn()).thenReturn(true);
    mActivity.onSomethingHappened();
    Button btn = (Button) mActivity.findViewById(R.id.logoutButton);
    String btnText = btn.getText().toString(); 
    assertTrue(btnText.equals("Log out"));  
}

}

vovkab

I have put everything together and made demo app that shows how to test with dagger: https://github.com/vovkab/dagger-unit-test

Here is my pervious answer with more details:
https://stackoverflow.com/a/24393265/369348

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!