LeoColman
05/07/2020, 9:51 PMany<Type>()
works as wellDiego Almeida de Oliveira
05/13/2020, 12:10 PMThomas Nordmeyer
05/19/2020, 10:34 AMevery {mocked.myProperty } returns "some"
mockes the getter. How do I do the same for the setter?rkeazor
05/21/2020, 4:10 PMLeoColman
06/05/2020, 5:52 PMPacane
06/08/2020, 7:45 PManswers ManyAnswersAnswer
with a listOf(ThrowingAnswer(myException), ConstantAnswer(myConstant))
but it doesn't seem to like having 2 types of answers in therethanksforallthefish
06/23/2020, 8:35 AM"f:can sync" {
val clock = mockk<Clock>()
every { clock.instant() } returns YESTERDAY andThen NOW
}
"f:can delete" {
val clock = mockk<Clock>()
every { clock.instant() } returns YESTERDAY andThen NOW andThen NOW
}
the first one logs:
10:33:28.099 [kotest-engine-0 @coroutine#2] DEBUG io.mockk.impl.instantiation.AbstractMockFactory - Creating mockk for Clock name=#1
10:33:29.228 [kotest-engine-0 @coroutine#2] DEBUG io.mockk.impl.instantiation.AbstractMockFactory - Creating mockk for Instant name=child of #1#2
the second one
10:33:29.245 [kotest-engine-0 @coroutine#3] DEBUG io.mockk.impl.instantiation.AbstractMockFactory - Creating mockk for Clock name=#3
10:33:29.247 [kotest-engine-0 @coroutine#3] DEBUG io.mockk.impl.instantiation.AbstractMockFactory - Creating mockk for Instant name=child of #3#4
how comes in the first test it takes one second to create a mock?poohbar
06/26/2020, 3:48 PMreevn
07/18/2020, 1:39 PMMockK
to run when using Kotest
4+ and running tests with the Kotest IntelliJ plugin: https://github.com/mockk/mockk/issues/459.
I'm not sure if this happens for everyone, but if it does then it is blocking all Kotest
users from really working with MockK
in an easy way. It is not clear to me if this is a problem that needs to be fixed on the Kotest
side or MockK
side.
Did anybody here experience the problem and have any more information or even workarounds? Please help us out in the issue or here on Slack 🙂.bbaldino
07/20/2020, 6:44 PMhuehnerlady
07/21/2020, 7:47 AMhuehnerlady
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.2bbaldino
08/26/2020, 11:14 PMclass Foo {
var field: Int = 0
}
fun main() {
val foo = spyk<Foo>()
foo.field = 42
// This works fine
verify { foo setProperty "field" value 42 }
// This should fail, but it throws "io.mockk.MockKException: can't find stub io.mockk.MockKMatcherScope$DynamicSetProperty@64a896b0"
verify { foo setProperty "field" wasNot Called }
}
Javier
09/01/2020, 12:04 PMio.mockk.MockKException: can't find function someMethod() for dynamic call
sealed class SomeSealed {
private fun someMethod() = ...
data class SomeData() : SomeSealed()
}
savrov
09/11/2020, 12:43 PMabstract class A(param1: Param1, param2: Param2) {
fun foo() {
val param1result = param1.get()
val param2result = param2.get()
val result = calculateResult(param1result, param2)
}
abstract fun calculateResult(param1: Param1, param2: Param2): Smth
}
Now, I want to test it. But I can not create an instance of A, since its a abstract class. The only way I managed to make it work is to create a real A class and use spyk function:
class AReal: A {
override fun calculateResult(...) {
...
}
}
class Test {
val aReal = spyk(AReal(...., .....))
}
Is there any other better way?Davide Giuseppe Farella
09/24/2020, 11:01 AMclass MyClass(val dir: File) {
fun stuff(filesNames: List<String>) {
...
val file = File(dir, fileName)
if (file.exists())
...
}
}
What would be the right way in order to pass the check if (file.exists())
?Vladyslav Sitalo
09/24/2020, 7:35 PMclearInvocations
in mockk? (clear invocation log without resetting the stubbing)Michael Pohl
09/29/2020, 7:28 AMcoEvery { mockRepo.saveToServer(mockArgument) } returns myResult
Now this will of course happen immediately. In reality it could take a moment. Now while this call is in flight, I flip a boolean to prevent issuing a second call while the first one is still underway. When it returns, I flip the boolean back. Is there a proper way to unit test this, preferably using Mockk
, of course...?tim
10/11/2020, 8:47 PMobject MockkInstant {
var value: Instant = Instant.now()
fun mock() {
mockkStatic(Instant::class)
every {
Instant.now()
} answers {
value.minusMillis(0) // line 22 from stack trace below
}
}
fun clear() {
clearStaticMockk(Instant::class)
}
fun step(duration: Duration = 1.seconds) {
value = value.add(duration) // extension function defined elsewhere
}
}
Now in my tests i'm trying to do this:
"fixes calls to Instant.now()" {
val a = Instant.now()
delay(1)
val b = Instant.now()
a.shouldBe(b)
}
But I'm getting an error from the answers
block:
Empty list doesn't contain element at index 0.
java.lang.IndexOutOfBoundsException: Empty list doesn't contain element at index 0.
at kotlin.collections.EmptyList.get(Collections.kt:35)
at kotlin.collections.EmptyList.get(Collections.kt:23)
at io.mockk.MockKAnswerScope.getValueAny(API.kt:3834)
at io.mockk.MockKAnswerScope.getValue(API.kt:2188)
at io.glimpseprotocol.testing.mockk.MockkInstant$mock$2.invoke(MockkInstant.kt:22)
at io.glimpseprotocol.testing.mockk.MockkInstant$mock$2.invoke(MockkInstant.kt:13)
at io.mockk.MockKStubScope$answers$1.invoke(API.kt:2092)
at io.mockk.MockKStubScope$answers$1.invoke(API.kt:2069)
at io.mockk.FunctionAnswer.answer(Answers.kt:19)
...
I've seen other people using returns, but afaik that returns the same Instant object whereas I want to create a new one each time Instant.now() is called. Any suggestions?Sergio Crespo Toubes
10/16/2020, 10:39 AMevery { manager.startFunction() } throws(error)
I don´t find any solution, can someone help me please? Thanks.poohbar
10/22/2020, 11:19 PMMockkKt.mockk()
but the mockk()
function does not exist 🤔thanksforallthefish
10/23/2020, 9:45 AM@Test // <- junit5 annotation
fun publisher() {
val publisher = mockk<Publisher>(relaxed = true)
UseCase(publisher).doSomething()
verify { publisher.publish(any<UseCase.Something>()) }
verify { publisher.publish(any<String>()) }
}
interface Publisher {
fun publish(any: Any)
fun publish(event: Event) {
publish(event as Any)
}
class Event(source: Any)
}
class UseCase(val publisher: Publisher) {
fun doSomething() {
publisher.publish(Something(""))
}
data class Something(val something: String)
}
the test passes. what am I not seeing?oleksiyp
11/13/2020, 8:27 PMsavrov
11/20/2020, 11:40 PMmockk()
and any()
. Why when i have smth like
coEvery { foo(any()) } returns SomeObject
it works. But when i replace any()
with mockk()
, its print out:
exception=io.mockk.MockKException: no answer found for: SomeClass(#9).foo$domain(ParamType(#5), continuation {})))
Kenneth
11/25/2020, 7:18 AMclass RequestSupplier : Supplier<Unit>
(nested class)?Tiger
11/30/2020, 7:52 PMTimur Atakishiev
12/05/2020, 10:42 AMinterface StorageService {
fun upload(inputStream: InputStream): String
}
class LocalStorageService: StorageService {
override upload(inputStream: InputStream): String {
//Some uploading logic
}
}
I want to verify that the function was called with the following code
verify {
storageService.putFile(
inputStream = withArg { assertEquals("Hello, World!".toByteArray(),it.readBytes())}
)
}
and I got an error, that my inputStreams byteArray is empty, can anyone help me pleasechristophsturm
12/05/2020, 12:41 PMio/mockk/proxy/jvm/dispatcher/JvmMockKDispatcher has been compiled by a more recent version of the Java Runtime (class file version 55.0), this version of the Java Runtime only recognizes class file versions up to 52.0
chao
01/03/2021, 4:55 PMhuehnerlady
01/13/2021, 4:59 PM*_
for that (see here)
Example
fun foo(p1: String, p2: Boolean, p3: Int): Boolean
I want to write something like this in a test:
every {foo(anyParameters())} returns true
Currently the only way I know is:
every {foo(any(), any(), any())} returns true
Is there a way to be less verbose and just say anything
?
I tried allAny()
as that sounded like what I wanted, but that didn’t help me either, for me it feels like this is the same as any()
?huehnerlady
01/13/2021, 4:59 PM*_
for that (see here)
Example
fun foo(p1: String, p2: Boolean, p3: Int): Boolean
I want to write something like this in a test:
every {foo(anyParameters())} returns true
Currently the only way I know is:
every {foo(any(), any(), any())} returns true
Is there a way to be less verbose and just say anything
?
I tried allAny()
as that sounded like what I wanted, but that didn’t help me either, for me it feels like this is the same as any()
?christophsturm
01/13/2021, 5:07 PMMattia Tommasone
01/13/2021, 8:11 PMfoo
method has two implementations, one taking two parameters and one taking three
which one would you be stubbing if you had anyParameters()
?huehnerlady
01/14/2021, 7:08 AM