我正在寻找等效于doReturn(...)。when(...)。*
我正在编写一些涉及很多系统类的单元测试(测试合同),因此需要拦截我无法控制的方法并返回一些回调(代码中的方法最终会返回) )。在mockito中,我可以做类似doReturn(...)。when(...)。*
的操作我无法在模拟K中找到类似的东西。似乎Every {}总是在答案或返回之前运行该块。
class Vehicle: Listener {
fun displayCar(listener:Listener){
OtherClass().fetchCar(listener)
}
override fun showCarSuccessful() {
//do something
}
}
class OtherClass {
//assume its an outside function that returns nothing but invokes a method of listener call back
fun fetchCar(listener: Listener) {
//... Some system level operations that I don't have control to generate mock objects but in the test I want to use the listener to call some method so that I can
// test some contracts
listener.showCarSuccessful()
}
}
class Tests {
@Test
fun testCarSuccess() {
val listener: Listener = mockk(relaxed = true)
val vehicle = Vehicle()
//also tried with mockkClass and others
val other: OtherClass = mockk(relaxed = true)
every { other.fetchCar(listener) } returns {listener.showCarSuccessful()}
vehicle.displayCar(listener)
//do some verification checks here
}
}
interface Listener {
fun showCarSuccessful()
}
答案 0 :(得分:2)
every{}
块是您的when
子句。您可以设置多个条件以返回不同的结果。请参见设置固定的returns
和执行程序化的answers
import io.mockk.MockKException
import io.mockk.every
import io.mockk.mockk
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test
class MyClass {
fun add(operand1: Int, operand2: Int): Int {
TODO()
}
}
class MockkTest {
@Test
fun testMocking() {
val myClassMock = mockk<MyClass> {
every { add(1, 2) } returns 3 // Set behaviour
every { add(2, 2) } returns 4 // Set behaviour
every { add(3, 4)} answers {args[0] as Int * args[1] as Int} // Programmatic behaviour
}
Assertions.assertEquals(3, myClassMock.add(1, 2))
Assertions.assertEquals(4, myClassMock.add(2, 2))
Assertions.assertEquals(12, myClassMock.add(3, 4))
Assertions.assertThrows(MockKException::class.java) {
myClassMock.add(5, 6) // This behaviour has not been set up.
}
}
}
但是,特别是在您的示例中,我找到以下行:
every { other.fetchCar(listener) } returns listener.showCarSuccessful()
很奇怪。首先,它没有按照您认为的去做-在设置此行为时将进行该调用,即告诉您的模拟返回该调用的结果,而不是执行此操作。要执行您想做的事情,您应该这样做:
every { other.fetchCar(listener) } answers {listener.showCarSuccessful()}
但是即使如此,在您调用被测类之后,该行仍在设置模拟行为-首先设置模拟行为。
此外,奇怪的是您在嵌套模拟中的顶级模拟中设置了副作用。确定要测试您的Vehicle
类,您要做的就是验证是否使用正确的参数调用了它的内部类。另外,Vehicle
如何获得对您的OtherClass
模拟的引用,它实例化了一个新的模拟并调用了该函数。
这是尝试使您的示例生效:
import io.mockk.mockk
import io.mockk.verify
import org.junit.jupiter.api.Test
interface Listener {
fun showCarSuccessful()
}
class Vehicle(val other: OtherClass) : Listener {
fun displayCar(listener: Listener) {
other.fetchCar(listener)
}
override fun showCarSuccessful() {
//do something
}
}
class OtherClass {
//assume its an outside function that returns nothing but invokes a method of listener call back
fun fetchCar(listener: Listener) {
}
}
class VehicleTest{
@Test
fun testDisplayCar(){
val listener: Listener = mockk(relaxed = true)
val other: OtherClass = mockk(relaxed = true) //also tried with mockkClass and others
val vehicle = Vehicle(other)
vehicle.displayCar(listener)
verify{ other.fetchCar(listener) }
}
}
即使是这样,我认为可能还有些偏离-我怀疑您希望Vehicle
传递给OtherClass
的侦听器本身,而不是参数...
然后,您还应该为OtherClass
编写一个单独的测试,以确保它在调用fetchCar
时达到预期的效果