Hi! What is the reason for such behaviour? ``` cl...
# mockk
m
Hi! What is the reason for such behaviour?
Copy code
class A

mockk<A> {
    every { toString() } returns “A” // ok
    every { hashCode() } returns 0 // fails with io.mockk.MockKException: Missing mocked calls inside every { ... } block
}

class B {
    override fun hashCode() = 42
}

mockk<B> {
    every { toString() } returns “A” // ok
    every { hashCode() } returns 0 // ok
}
Why mockk cannot mock hashCode() if it is not overriden in class explicitly?
m
i guess the real question would be “why would you want to mock hashCode in the first place?”
m
Actually I need to mock equals method. To make comparable mock instance. That is how I faced this problem. Comparable mocks are very useful for testing android classes, without using Robolectric and running tests on host machine without real android.jar implementation.
m
i don’t think mocking equals is a good idea either it’s used internally by mockk and mocking it may cause weird issues
m
As I know mocks use default equals. What kind of problems, any samples? This gives many positive results, so currently I dont want to refuse it without clear reasons.
My equals implementation does not break java contract so it should work even in library environment (as I expect)..
Even with some problems on mockk side, i can implement something similar on my own with java dynamic proxies. So I am more interested in original question why mockk cant mock hashCode() if it is not overriden explicitly?
114 Views