huehnerlady
07/23/2020, 7:10 AMimport io.mockk.every
import io.mockk.mockk
import io.mockk.verify
import org.junit.jupiter.api.DynamicTest
import org.junit.jupiter.api.TestFactory
class Test {
private val testService: TestService = mockk(relaxed = true)
private val testStrings = listOf("1", "2")
class TestService {
fun test(string: String): String {
return string
}
}
@TestFactory
fun tests() = testStrings
.map { input: String ->
DynamicTest.dynamicTest("should call service once for '$input'") {
every {
testService.test(any())
} returns "foo"
testService.test(input)
verify(exactly = 1) { testService.test(any()) }
}
}
}
As you can see in the test, I am calling the mock just once. still, the testoutput is as shown on the screenshot. The second tests always fails.
What am I doing wrong here? Can anybody help me? 🙂
I am using mokk 1.10.0, kotlin 1.3.72 and junit 5.6.2thanksforallthefish
07/23/2020, 7:46 AM@AfterEach
can help you
fun @AfterEach() cleanUp { clearAllMocks() }
otherwise you can just add clearAllMocks()
after your verify
EDIT: forgot fun name 🙂huehnerlady
07/23/2020, 8:05 AMwhere
. But I guess I have to look furtherchristophsturm
07/23/2020, 10:07 AMthanksforallthefish
07/23/2020, 10:57 AMchristophsturm
07/23/2020, 11:07 AMhuehnerlady
07/23/2020, 11:07 AMchristophsturm
07/23/2020, 11:11 AMsam
07/25/2020, 5:07 PMclass Test : FunSpec() {
init {
val testStrings = listOf("1", "2")
testStrings.forEach { input ->
test("should call service once for '$input'") {
val testService: TestService = mockk(relaxed = true)
every {
testService.test(any())
} returns "foo"
testService.test(input)
verify(exactly = 1) { testService.test(any()) }
}
}
}
class TestService {
fun test(string: String): String {
return string
}
}
}
class Test : FunSpec() {
init {
test("should call service once") {
forAll(
row("1", true, 5.4),
row("2", false, 14.2)
) { a, b, c ->
val testService: TestService = mockk(relaxed = true)
every {
testService.test(any())
} returns "foo"
testService.test(a, b, c)
verify(exactly = 1) { testService.test(any()) }
}
}
}
class TestService {
fun test(string: String, boolean: Boolean, double: Double): String {
return string
}
}
}
huehnerlady
07/27/2020, 5:55 AMsam
07/27/2020, 6:56 AMhuehnerlady
07/27/2020, 7:07 AM