Hi everyone, I’m dealing with an issue when trying to spy my class contains a value class inside the...
l
Hi everyone, I’m dealing with an issue when trying to spy my class contains a value class inside the constructor with
spyk
which provided by
Mockk
The code as below:
Copy code
@JvmInline
value class MyValueClass(Val value: String = “”) : Serializable

class A(
   Val valueClass: MyValueClass = MyValueClass()
)

// In unit test file
@Test
fun test() {
    Val mockClass = spyk<A>()
    …
}
And this error happened
Copy code
io.mockk.MockKException: Can't instantiate proxy via default constructor for class A
Appreciate with your help 🙏
p
I haven't used mockk in quite a bit of time but I believe spyk receives an instance. Create the A() instance and pass it to spyk()
l
It works, thanks @Pablichjenkov 🙌 Although I don’t understand why this way works 🤷‍♂️ Can anyone help explain it, please 🙏
p
Is the purpose of a spy. In a spy you provide an implementation with actual functions and mock part of it, a few of them. In contrast, a mock, mocks the full class functions.
You can research more on mock vs spy
l
Yup, I understand about the mock and spy. Just don’t know why in my case spy(A()) works and spyk<A>() doesn’t
In the
MockK
document, they have the same meaning in the case of the default constructor tho
p
I got you now, humm, is probably due to the way it works internal. Mocking/spying an interface or abstract or open class may be based on implementing an instance of these 3. But since the class is final the library is not able to extend it. I guess is related to that but maybe was a design decision.
1215 Views