测试用例单独运行良好,但是当我同时运行所有测试用例时,其中一些失败。
我试图使用Mockito.reset函数来重置模拟,但是它不起作用
那么,我该怎么做才能重置模拟?
注意:我正在使用嘲笑2.21.0
class MainPresenterTest {
private lateinit var api: Api
private lateinit var mainViewContract: MainActivity
private lateinit var presenter: MainPresenter
@Before
fun setup() {
api = Mockito.mock(Api::class.java)
mainViewContract = Mockito.mock(MainActivity::class.java)
presenter = MainPresenter(mainViewContract, api)
}
@After
fun resetMocks() {
reset(api, mainViewContract)
StrictMode.setThreadPolicy(StrictMode.ThreadPolicy.Builder().build())
}
@Test
fun changeShiftToOn1() {
Shared.shiftState = ShiftStatus.OFF
val postShiftResponse = PostShiftResponse(200, "132")
Mockito.`when`(api.startShift()).thenReturn(postShiftResponse)
Mockito.`when`(mainViewContract.popupErrorMessage()).then { }
Mockito.`when`(mainViewContract.showShiftDialog()).then { }
Mockito.`when`(mainViewContract.dismissDialog()).then { }
Mockito.`when`(mainViewContract.showProgressDialog()).then { }
presenter.changeShiftState()
Mockito.verify(mainViewContract, Mockito.atLeastOnce()).showConnectingBar()
Mockito.verify(mainViewContract, Mockito.atLeastOnce()).startServices()
Mockito.verify(mainViewContract, Mockito.atLeastOnce()).setStartShiftLayout(false)
}
@Test
fun changeShiftToOn2() {
Shared.shiftState = ShiftStatus.OFF
val postShiftResponse = PostShiftResponse(400, "132")
Mockito.`when`(api.startShift()).thenReturn(postShiftResponse)
Mockito.`when`(mainViewContract.popupErrorMessage(null)).then { }
Mockito.`when`(mainViewContract.showShiftDialog()).then { }
Mockito.`when`(mainViewContract.dismissDialog()).then { }
Mockito.`when`(mainViewContract.showProgressDialog()).then { }
presenter.changeShiftState()
Mockito.verify(mainViewContract, Mockito.atLeastOnce()).popupErrorMessage(null)
Mockito.verify(mainViewContract, Mockito.atLeastOnce()).showShiftDialog()
}
}
答案 0 :(得分:0)
如果您要执行一些异步任务,请确保在异步操作完成后执行下一条指令。
为了等待异步任务,有一个名为Awaitility的库。 这是链接; https://github.com/awaitility/awaitility.git
您也可以像我一样使用它, // SUT正在测试系统 SUT.login()
await().until {
SUT.coroutineContext.isActive
}