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?
Error:(14, 21) error: cannot find symbol variable DaggerFakeCorePdfComponentAs for the tests I would like to test the fragment itself but it will not run without the dependencies.aptplugin and generate the classes for you. Have you tried addingtestCompile <dagger-compiler>in addition toapt? If not can you give it a shot and let me know here pls.testCompile 'org.hamcrest:hamcrest-core:1.1' testCompile 'org.hamcrest:hamcrest-library:1.1' testCompile 'org.hamcrest:hamcrest-integration:1.1' testCompile "org.robolectric:robolectric:3.1.1" testCompile "org.robolectric:shadows-support-v4:3.1.1" compile 'com.google.dagger:dagger:2.6.1' apt "com.google.dagger:dagger-compiler:2.6.1"Do you want me to addtestcompile "com.google.dagger:dagger-compiler:2.6.1"at the end? It gives meError:(50, 0) Could not find method testcompile() for arguments [com.google.dagger:dagger-compiler:2.6.1]testCompilewith capitalC:)