Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error msg missing type information when no answers found for an overloaded method #1219

Open
3 tasks done
jatsek opened this issue Feb 15, 2024 · 2 comments
Open
3 tasks done

Comments

@jatsek
Copy link

jatsek commented Feb 15, 2024

Prerequisites

Please answer the following questions for yourself before submitting an issue.

  • I am running the latest version
  • I checked the documentation and found no answer
  • I checked to make sure that this issue has not already been filed

Current Behavior

Given this pseudocode:

interface Item
class ItemA: Item
class ItemB: Item

fun process(item: Item)
fun process(item: ItemA)
fun process(item: ItemB)

My code was erasing the type of ItemA somewhere and, while methods process(ItemA) and process(ItemB) were mocked, process(Item) was not and mockk was rightfully throwing an error.

However error message that in this case says:

no answer found for process(ItemA) among the configured answers: 
process(eq(ItemA))
process(eq(ItemB))

Expected Behavior

Maybe some type information or just full method signatures could be included with this particular error?

Context

  • MockK version: 1.13.9
  • OS: Mac
  • Kotlin version: 1.9.22
  • JDK version: 21
  • JUnit version: 4.13.2
  • Type of test: unit test

Stack trace

// -----------------------[ YOUR STACK STARTS HERE ] -----------------------
io.mockk.MockKException: no answer found for Processor(#1).process(InputDataA) among the configured answers: (Processor(#1).process(eq(InputDataA)))
Processor(#1).process(eq(InputDataB))))
	at app//io.mockk.impl.stub.MockKStub.defaultAnswer(MockKStub.kt:91)
	at app//io.mockk.impl.stub.MockKStub.answer(MockKStub.kt:42)
	at app//io.mockk.impl.recording.states.AnsweringState.call(AnsweringState.kt:16)
	at app//io.mockk.impl.recording.CommonCallRecorder.call(CommonCallRecorder.kt:53)
	at app//io.mockk.impl.stub.MockKStub.handleInvocation(MockKStub.kt:269)
	at app//io.mockk.impl.instantiation.JvmMockFactoryHelper$mockHandler$1.invocation(JvmMockFactoryHelper.kt:24)
	at app//io.mockk.proxy.jvm.advice.Interceptor.call(Interceptor.kt:21)
// -----------------------[ YOUR STACK TRACE ENDS HERE ] -----------------------

Minimal reproducible code (the gist of this issue)

package io.mockk.gh

import io.mockk.every
import io.mockk.mockk
import org.junit.Test


class Processor {
    fun process(data: InputData): Int = 1
    fun process(data: InputDataA): Int = 2
    fun process(data: InputDataB): Int = 3
}

interface InputData
data object InputDataA: InputData
data object InputDataB: InputData

class TestSubject(private val processor: Processor) {
    fun run() = listOf(
            InputDataA,
            InputDataB
    ).sumOf {
        processor.process(it)
    }
}

class SomeTest {

    val processorMock: Processor = mockk()

    @Test
    fun test() {
        every { processorMock.process(InputDataA) } returns 20
        every { processorMock.process(InputDataB) } returns 30

        TestSubject(processorMock).run()
    }
}
@ddaakk
Copy link

ddaakk commented Feb 22, 2024

Do you expect like this result?

io.mockk.MockKException: no answer found for Processor(#1).process(io.mockk.junit4.InputDataA@3c904f1e): Int among the configured answers
Processor(#1).process(eq(io.mockk.junit4.InputDataB@4682eba5))))
	at io.mockk.impl.stub.MockKStub.defaultAnswer(MockKStub.kt:91)
	at io.mockk.impl.stub.MockKStub.answer(MockKStub.kt:42)
	at io.mockk.impl.recording.states.AnsweringState.call(AnsweringState.kt:16)
	at io.mockk.impl.recording.CommonCallRecorder.call(CommonCallRecorder.kt:53)
	at io.mockk.impl.stub.MockKStub.handleInvocation(MockKStub.kt:269)
	at io.mockk.impl.instantiation.JvmMockFactoryHelper$mockHandler$1.invocation(JvmMockFactoryHelper.kt:24)
	at io.mockk.proxy.jvm.advice.Interceptor.call(Interceptor.kt:21)

@jatsek
Copy link
Author

jatsek commented Feb 22, 2024

Looks great!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants