我有一个测试如下,在给定的条件下,我想确保 mainPresenter.presenterFunction()
没有被调用。
class MainPresenterTest {
val mainPresenter: MainPresenter
val mainView: MainView
val mainBridge: MainBridge
init {
mainView = mock(MainView::class.java)
webBridge = mock(MainBridge::class.java)
mainPresenter = MainPresenter(mainView, mainBridge)
}
@Before
fun setUp() {
MockitoAnnotations.initMocks(this)
}
@Test
fun simpleTeset1() {
// Given
whenMock(mainView.viewFunctionCondition()).thenReturn(true)
// When
mainPresenter.onTriggger()
// Then
verify(mainView).viewFunction1()
verify(mainPresenter, never()).presenterFunction()
verify(mainView, never()).viewFunction2()
}
}
但是它错误地指出
org.mockito.exceptions.misusing.NotAMockException:
Argument passed to verify() is of type MainPresenter and is not a mock!
Make sure you place the parenthesis correctly!
错误在verify(mainPresenter, never()).presenterFunction()
这是预期的,因为 mainPresenter
不是模拟对象。我如何测试为非模拟对象调用的方法?
我在 how to verify a method of a non-mock object is called? 中看到了答案,但那仍在使用 Mock 和 Spy。我希望找到一种无需模拟我已有的类实例的方法。
(注:上面是用Kotlin写的)
最佳答案
根据定义,这是行不通的。
模拟框架只能验证
对模拟对象的调用。他们无法知道他们无法控制的对象发生了什么或没有发生什么。您要么需要模拟您的演示者,将其替换为 stub ,要么……
好吧,我认为这是唯一的两个选择。
关于android - 我如何测试没有为非模拟对象调用方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37047745/