Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit testing while using Dagger 2 (Robolectric and Mockito)

I'm trying to write some tests for fragments which have fields annotated with @Inject. For example, a chunk of my app looks like this:

Module:

@Module
public class PdfFactoryModule {

    @Provides @Singleton
    PdfFactory providePdfFactory() {
        return PdfFactory.getPdfFactory();
    }
}

Component:

@Singleton
@Component(modules = PdfFactoryModule.class)
public interface CorePdfComponent {
    void inject(PagerFragment pagerFragment);
}

Application:

public class CorePdfApplication extends Application {
    @NonNull
    private CorePdfComponent component;

    @Override
    public void onCreate() {
        super.onCreate();
        component = DaggerCorePdfComponent.builder().build();
    }

    @NonNull
    public CorePdfComponent getComponent() {
        return component;
    }

}

PagerFragment:

public class PagerFragment extends Fragment {
@Inject PdfFactory pdfFactory;

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //Dagger 2
    ((CorePdfApplication) getActivity().getApplication()).getComponent().inject(this);
}

(Note that these are only snippets of my whole code, I'm showing only the essentials for this particular dependency to keep it clear.)


I was trying to do a test like this:

Fake Module:

@Module
public class FakePdfFactoryModule extends PdfFactoryModule {

    @Override
    PdfFactory providePdfFactory() {
        return Mockito.mock(PdfFactory.class);
    }
}

Fake Component:

@Singleton
@Component(modules = FakePdfFactoryModule.class)
public interface FakeCorePdfComponent extends CorePdfComponent {
    void inject(PagerFragmentTest pagerFragmentTest);
}

Fake Application:

public class FakeCorePdfApplication extends CorePdfApplication {
    @NonNull
    private FakeCorePdfComponent component;

    @Override
    public void onCreate() {
        super.onCreate();
        component = DaggerFakeCorePdfComponent.builder().build();
    }

    @NonNull
    public FakeCorePdfComponent getComponent() {
        return component;
    }
}

Test:

@RunWith(RobolectricTestRunner.class)
@Config(constants = BuildConfig.class, sdk = 21, application = FakeCorePdfApplication.class)
public class PagerFragmentTest {

    PagerFragment pagerFragment;

    @Before
    public void setup() {
        pagerFragment = new PagerFragment();
        startVisibleFragment(pagerFragment);
    }

    @Test
    public void exists() throws Exception {
        assertNotNull(pagerFragment);
    }

But the DaggerFakeCorePdfComponent doesn't generate. I may have messed up big time because I never tested with dependency injection. What am I doing wrong?

like image 990
Haruspik Avatar asked Sep 12 '16 17:09

Haruspik


2 Answers

My advice - "Do not use dagger in tests".

Just change your code to next:

public class FakeCorePdfApplication extends CorePdfApplication {
    @NonNull
    private CorePdfComponent component = mock(CorePdfComponent.class);

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @NonNull
    public CorePdfComponent getComponent() {
        return component;
    }
}

And:

@RunWith(RobolectricTestRunner.class)
@Config(constants = BuildConfig.class, sdk = 21, application = FakeCorePdfApplication.class)
public class PagerFragmentTest {

    PagerFragment pagerFragment;

    @Before
    public void setup() {
        pagerFragment = new PagerFragment();
        CorePdfComponent component = ((CorePdfApplication)RuntimeEnvironment.application).getComponent();

        doAnswer( new Answer() {
          Object answer(InvocationOnMock invocation) {
           fragment. pdfFactory = mock(PdfFactory.class);
           return null;
          }
        }).when(component).inject(pageFragment);
        startVisibleFragment(pagerFragment);
    }

    @Test
    public void exists() throws Exception {
        assertNotNull(pagerFragment);
    }
}
like image 63
Eugen Martynov Avatar answered Nov 15 '22 04:11

Eugen Martynov


You may try:

androidTestApt "com.google.dagger:dagger-compiler:<version>"

I was having similar problem, it worked for me.

like image 41
Abdul Wadood Avatar answered Nov 15 '22 04:11

Abdul Wadood