【发布时间】:2022-01-11 18:13:21
【问题描述】:
我是 Dagger 和 Mockito 的新手。我正在尝试在单元测试中使用 Dagger 模块中定义的构造函数,以便使用默认值创建对象。
这是模块:
@模块 类 AutoCloseCountDownTimerModule {
@Provides
@Singleton
fun getAutoCloseCountDownTimer(
userInteractionClient: UserInteractionClient,
rxPositionManager: RxPositionManager
): AutoCloseCountDownTimer {
return AutoCloseCountDownTimer(userInteractionClient, rxPositionManager, 15000, 45000)
}
这就是我尝试在单元测试中模拟 AutoCloseCountDown 类的方式:
@RunWith(MockitoJUnitRunner.class) 公共类 AutoCloseCountDownTimerTest {
@Mock
private AutoCloseCountDownTimer autoCloseCountDownTimer;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
}
@Test
public void basicTest() {
final AutoCloseCountDownTimer.Listener mockListener = Mockito.mock(AutoCloseCountDownTimer.Listener.class);
autoCloseCountDownTimer.registerListener(mockListener);
final int expectedValue = 10;
autoCloseCountDownTimer.notifyOnAutoClose(expectedValue);
Mockito.verify(mockListener).onAutoClose(expectedValue);
如何实现 autoclosedCountDownTimer 将在 Test 中使用 dagger 预定义的值?
【问题讨论】:
标签: java android junit mockito dagger-2