Mattia Tommasone
08/15/2023, 6:51 PMHéctor Alpízar
08/16/2023, 4:29 AMMattia Tommasone
09/12/2023, 5:03 PMTiago
09/14/2023, 12:58 PMBad recording sequence. State: AnsweringState
io.mockk.MockKException: Bad recording sequence. State: AnsweringState
Jonas
09/19/2023, 10:05 AMPlanning to publish a release later this week, likely tomorrow or Friday
Mattia Tommasone
09/19/2023, 10:07 AMxenomachina
09/19/2023, 11:43 PMevery { implementedMethod() } answers { callOriginal() }
for every single implemented method/property?Mattia Tommasone
09/22/2023, 1:46 PMJonathan Lennox
09/28/2023, 9:40 PMblock()
, it throws with the same exception, java.lang.ClassCastException: class java.lang.Long cannot be cast to class org.jitsi.nlj.util.Bandwidth (java.lang.Long is in module java.base of loader 'bootstrap'; org.jitsi.nlj.util.Bandwidth is in unnamed module of loader 'app')
. (This is with the latest mockk, 1.13.8.). Is this a known issue? Any idea what I can look at either to fix it or to work around it?Jonathan Lennox
09/29/2023, 3:47 PMpackage org.example
import io.kotest.core.spec.style.ShouldSpec
import io.kotest.matchers.shouldBe
import io.mockk.every
import io.mockk.spyk
@JvmInline
value class LongWrapper(val value: Long)
class StateHolder() {
var state = -1L
fun setState(v: LongWrapper?) {
state = v?.value ?: -1L
}
fun getState(): LongWrapper? {
return if (state < 0) { null } else {
LongWrapper(state)
}
}
}
class MockTest: ShouldSpec() {
init {
context("Mocking a function") {
should("work correctly") {
val mockObj = spyk<StateHolder>()
every { mockObj.getState() } returns LongWrapper(42)
mockObj.getState() shouldBe LongWrapper(42)
}
}
}
}
Matthias Geisler
10/14/2023, 10:36 PMMattia Tommasone
10/15/2023, 1:35 PMMatthias Geisler
10/15/2023, 9:36 PMalbrechtroehm
10/20/2023, 3:09 PMKamilH
11/08/2023, 2:01 PMsuspend fun <T> execute(work: suspend Scope.() -> T): Result<T> =
Result.runCatching {
scope.work()
}
in the test, I would like to mock Scope
(full example in the thread)Carlos Tasada
11/21/2023, 10:52 AMval publisher = mockk<CommandPublisher>(relaxed = true)
val something = Something(publisher)
val request = Request(field = "a")
something.doSomething(request)
verify {
publisher.publish(any())
}
This works fine. But I want to confirm that the published object is the one I expect
verify {
publisher.publish(Event(request))
}
Where Event(request)
maps some values to the published object.
This check fails, because "arguments are not matching" when looking, the issue seems that verify
is trying to compare by instance, but the expected instance is different. I want to compare by content. I saw that I could use cmpEq
but I cannot find how to use it.
Can someone provide an example of verify
and cmpEq
?
Thanks a lot for your helpFunyinoluwa Kashimawo
11/21/2023, 10:04 PMSuppressed: java.lang.ClassCastException: class java.lang.Long cannot be cast to class kotlin.time.Duration (java.lang.Long is in module java.base of loader 'bootstrap'; kotlin.time.Duration is in unnamed module of loader 'app')
I see that my issue is similar to this but there is no solution there
https://github.com/mockk/mockk/issues/869Mahendra Sutar
12/07/2023, 6:24 AMLevi
12/19/2023, 7:02 PMio.kotest.assertions.AssertionFailedError: expected:MyType but was:kotlin.Any<Any(child^2 of #1#4#5)>
Levi
12/19/2023, 7:03 PMLevi
12/19/2023, 7:27 PMcallOriginal()
David Kubecka
12/20/2023, 11:01 AMdata class Animal(val type: String) {
val isFavorite = type == "cat"
}
How do you set up the class in a test which just relies on its behavior, i.e. it's not the test for the class? Three approaches come to my mind:
• Just construct the class explicitly and let the isFavorite
property be actually computed.
• Mock the class including all its properties (constructor and computed)
• Use spyk
and mock just the computed property (didn't try but I guess it should work)David Kubecka
12/22/2023, 4:01 PMverify { instance.method(MyClass(any(), true)) } returns ...
Currently, I get left matchers: [any()]
Mattia Tommasone
01/08/2024, 10:12 AMKlitos Kyriacou
01/24/2024, 3:27 PMclass ClassBeingMocked(val arg: String) {
fun foo(n: Int = arg.length) {
println("foo($n) called")
}
}
class ClassBeingTested(val a: ClassBeingMocked) {
fun bar() {
a.foo()
}
}
class MyJunitTest {
val a = mockk<ClassBeingMocked>(relaxed = true)
val b = ClassBeingTested(a)
@Test
fun test1() {
b.bar()
}
}
Notice that ClassBeingMocked.foo
takes a default parameter that depends on a property given as constructor parameter. The mock instance doesn't use this constructor parameter, as it's a mock. ClassBeingTested
relies on the default value of this function.
The above test throws NullPointerException: Cannot invoke "String.length()" because "<parameter1>.arg" is null
. The stack trace starts at the line where foo
is implemented, in class ClassBeingMocked
.
If a
is a mock instance, why does the stack trace show that it's going to the real class? Furthermore, can I somehow fix this without changing ClassBeingTested
to pass an actual parameter instead of the default?Eduard Boloș
01/31/2024, 6:34 PMclass Sut(val dep1: Dep1, val dep2: Dep2) {
fun foo(): A {
dep1.bar()
return dep2.baz()
}
}
class Dep1 {
fun bar() {
// ...
}
}
class Dep2 {
fun baz(): A {
// ...
}
}
class SutTest {
val dep1 = mockk<Dep1>()
val dep2 = mockk<Dep2>()
val sut = Sut(dep1, dep2)
@Test
fun `test foo`() {
every { dep2.baz() } returns A()
sut.foo() // <-- this calls dep1.bar, which is not stubbed, and yet it doesn't throw
verify { dep1.bar() }
}
Shouldn't sut.foo()
throw because it calls the non-stubbed dep1.bar()
, since we didn't set relaxed = true
on it?df
02/03/2024, 9:48 AMval libriOrder = mockk<LibriOrder> {
every { blueOrderUuid } returns UUID.randomUUID()
every { items } returns libriOrderItems
}
subject.libriOrderItemCancelled(...)
verify {
blueOrderService.cancelOrderItem(
libriOrder.blueOrderUuid,
cancelledLibriOrderItem.blueOrderItemId
)
}
Nick
02/04/2024, 7:00 PMIterable
. wondering if this is a known bug and if there's any work-around.
private interface Fails: Iterable<Int>
@Test fun `fails debug`() {
mockk<Fails> {} // <----------- Just doing this results in the following exception
}
class redefinition failed: attempted to change the schema (add/remove fields)
java.lang.UnsupportedOperationException: class redefinition failed: attempted to change the schema (add/remove fields)
at java.instrument/sun.instrument.InstrumentationImpl.retransformClasses0(Native Method)
at java.instrument/sun.instrument.InstrumentationImpl.retransformClasses(InstrumentationImpl.java:169)
at io.mockk.proxy.jvm.transformation.JvmInlineInstrumentation.retransform(JvmInlineInstrumentation.kt:28)
at io.mockk.proxy.common.transformation.RetransformInlineInstrumentation$doCancel$1.invoke(RetransformInlineInstrumentation.kt:38)
at io.mockk.proxy.common.transformation.RetransformInlineInstrumentation$doCancel$1.invoke(RetransformInlineInstrumentation.kt:32)
at io.mockk.proxy.common.transformation.ClassTransformationSpecMap.applyTransformation(ClassTransformationSpecMap.kt:41)
at io.mockk.proxy.common.transformation.RetransformInlineInstrumentation.doCancel(RetransformInlineInstrumentation.kt:32)
at io.mockk.proxy.common.transformation.RetransformInlineInstrumentation.access$doCancel(RetransformInlineInstrumentation.kt:6)
at io.mockk.proxy.common.transformation.RetransformInlineInstrumentation$execute$1$1.invoke(RetransformInlineInstrumentation.kt:17)
at io.mockk.proxy.common.transformation.RetransformInlineInstrumentation$execute$1$1.invoke(RetransformInlineInstrumentation.kt:17)
at io.mockk.proxy.common.transformation.RetransformInlineInstrumentation.execute(RetransformInlineInstrumentation.kt:23)
at io.mockk.proxy.jvm.ProxyMaker.inline(ProxyMaker.kt:90)
at io.mockk.proxy.jvm.ProxyMaker.proxy(ProxyMaker.kt:34)
at io.mockk.impl.instantiation.JvmMockFactory.newProxy(JvmMockFactory.kt:34)
at io.mockk.impl.instantiation.AbstractMockFactory.newProxy$default(AbstractMockFactory.kt:24)
at io.mockk.impl.instantiation.AbstractMockFactory.mockk(AbstractMockFactory.kt:59)
Asaf Peleg
02/13/2024, 9:46 PMEmil Kantis
02/29/2024, 1:40 PM