我正试图为带有@Inject注释的字段的片段编写一些测试。例如,我的应用程序的一个块看起来像这样:
模块:
@Module
public class PdfFactoryModule {
@Provides @Singleton
PdfFactory providePdfFactory() {
return PdfFactory.getPdfFactory();
}
}
组件:
@Singleton
@Component(modules = PdfFactoryModule.class)
public interface CorePdfComponent {
void inject(PagerFragment pagerFragment);
}
应用程序:
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);
}
(注意,这些只是我整个代码的一小部分,为了保持清晰,我只展示了这个特定依赖项的关键部分。)
我想做一个这样的测试:
假模块:@Module
public class FakePdfFactoryModule extends PdfFactoryModule {
@Override
PdfFactory providePdfFactory() {
return Mockito.mock(PdfFactory.class);
}
}
假组件:@Singleton
@Component(modules = FakePdfFactoryModule.class)
public interface FakeCorePdfComponent extends CorePdfComponent {
void inject(PagerFragmentTest pagerFragmentTest);
}
假的应用程序:
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;
}
}
测试:@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);
}
但是DaggerFakeCorePdfComponent不生成。我可能在很大程度上搞砸了,因为我从未使用依赖注入进行测试。我做错了什么?
我的建议- "不要在测试中使用dagger "。
把你的代码改成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;
}
}
:
@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);
}
}
你可以试试:
androidTestApt "com.google.dagger:dagger-compiler:<version>"
我也有类似的问题,它对我有效