gpt4 book ai didi

android - 使用 Dagger 2(Robolectric 和 Mockito)进行单元测试

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:12:21 25 4
gpt4 key购买 nike

我正在尝试为具有用@Inject 注释的字段的 fragment 编写一些测试。例如,我的应用程序的一部分如下所示:

模块:

@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;
}

}

寻呼机 fragment :

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);
}

(请注意,这些只是我整个代码的 fragment ,为了清楚起见,我只展示了这个特定依赖项的基本要素。)


我正在尝试做这样的测试:

假模块:

@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 不会生成。我可能把时间搞砸了,因为我从未测试过依赖注入(inject)。我做错了什么?

最佳答案

我的建议 - “不要在测试中使用 Dagger ”。

只需将您的代码更改为下一步:

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);
}
}

关于android - 使用 Dagger 2(Robolectric 和 Mockito)进行单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39455452/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com