import static org.mockito.Mockito.atLeast;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
verify(mockObject, atLeast(2)).someMethod("was called at least twice");
verify(mockObject, times(3)).someMethod("was called exactly three times");
interface MyCallback {
fun someMethod(value: String)
}
class MyTestableManager(private val callback: MyCallback) {
fun perform(){
callback.someMethod("first")
callback.someMethod("second")
callback.someMethod("third")
}
}
型 测试:
import com.nhaarman.mockitokotlin2.times
import com.nhaarman.mockitokotlin2.verify
import com.nhaarman.mockitokotlin2.mock
...
val callback: MyCallback = mock()
val uut = MyTestableManager(callback)
uut.perform()
val captor: KArgumentCaptor<String> = com.nhaarman.mockitokotlin2.argumentCaptor<String>()
verify(callback, times(3)).someMethod(captor.capture())
assertTrue(captor.allValues[0] == "first")
assertTrue(captor.allValues[1] == "second")
assertTrue(captor.allValues[2] == "third")
2条答案
按热度按时间lyr7nygr1#
使用适当的VerificationMode:
字符串
yjghlzjz2#
对于Kotlin:
构建gradle:
字符串
代码:
型
测试:
型
对于Java:
Lombok用来简化。如果你愿意,你也可以键入构造函数。
构建gradle:
型
代码:
型
测试:
型