Posted on Leave a comment

Android dagger override Module in test

overriding @Module classes with Dagger 2 [in tests]

ActivityTestRule, Building Instrumented Unit Tests, JUnit4 Rules with Testing Support Library, Testing Support Library, Getting Started with Testing

Example:

Main application code

<br>
public class MainApplication extends Application {</p>
<p>    protected MainAppComponent initComponent() {<br>
        return DaggerMainAppComponent.builder()<br>
                .applicationModule(new ApplicationModule(this))<br>
                .build();<br>
    }</p>
<p>    @Override<br>
    public void onCreate() {<br>
        mComponent = initComponent();<br>
    }<br>
}<br>

test application code

<br>
public class TestApplication extends MainApplication {</p>
<p>    @Override<br>
    protected TestAppComponent initComponent() {<br>
        return DaggerTestAppComponent.builder()<br>
                .applicationModule(new TestApplicationModule(this))<br>
                .build();<br>
    }<br>
}<br>

Component code

<br>
public interface TestAppComponent extends MainAppComponent {<br>
    //code<br>
}<br>

Main module code

<br>
@Module<br>
public class ApplicationModule {</p>
<p>    @Singleton<br>
    @Provides<br>
    public IFooManager provideFooManager() {<br>
        // our code<br>
    }<br>
}<br>

Test module code

<br>
public class TestApplicationModule extends ApplicationModule {</p>
<p>    @Override<br>
    public IFooManager provideFooManager() {<br>
        // our code of mock manager<br>
    }<br>
}<br>

AndroidJUnitRunner

<br>
public class TestApplicationRunner extends AndroidJUnitRunner {<br>
    @Override<br>
    public Application newApplication(ClassLoader cl, String className, Context context) throws IllegalAccessException, ClassNotFoundException, InstantiationException {<br>
        return super.newApplication(cl, TestApplication.class.getName(), context);<br>
    }<br>
}<br>

and put the runner to module build.gradle

<br>
def DAGGER_VERSION = '2.10'<br>
dependencies {<br>
    testApt "com.google.dagger:dagger-compiler:$DAGGER_VERSION"<br>
    androidTestApt "com.google.dagger:dagger-compiler:$DAGGER_VERSION"<br>
}<br>
android {<br>
  defaultconfig {<br>
    // Test instrumentation configurations<br>
        testApplicationId "com.myapp.test"<br>
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"<br>
        testInstrumentationRunner "uitesting.core.runner.TestApplicationRunner"<br>
        testHandleProfiling true<br>
        testFunctionalTest true<br>
  }<br>
}<br>

test class code

<br>
@LargeTest<br>
@RunWith(AndroidJUnit4.class)<br>
public class RegistrationTest {</p>
<p>    @Rule<br>
    public ApplicationTestRule&lt;TestApplication&gt; appRule = new ApplicationTestRule&lt;&gt;(TestApplication.class);</p>
<p>    @Inject<br>
    IFooManager manager;</p>
<p>    @Before<br>
    public void setUp() {<br>
        TestApplication app;<br>
        app = (TestApplication) InstrumentationRegistry.getInstrumentation().getTargetContext().getApplicationContext();<br>
        ((TestAppComponent)(app).getComponent()).inject(this);<br>
    }<br>
}<br>


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.