Mockito-Scala方法调用验证失败求助,提交伪Issue仍未解决
Let's walk through the issues here and get your test passing step by step:
1. The Critical Typo: Method Name Mismatch
First off, there's a glaring mistake that's almost definitely causing your failure. Let's connect the dots:
- Your
ServiceProxyexplicitly callsservice.execute(state, p, new ThirdParameter()) - You stubbed the
executemethod on your mock:mocked.execute(*, *, *) answers ... - But you're trying to verify calls to
mocked.method(*, *, *)
Your interaction log even shows three calls to mocked.method(...), which doesn't line up with your proxy code at all. Either you meant to verify execute instead of method, or there's a mix-up in your Service interface's method names. Fixing this method name mismatch will resolve the "Wanted but not invoked" error immediately.
2. Ensure You're Using Mockito-Scala's Matchers
Mockito-Scala requires you to import its sugar matchers to use wildcards like * correctly. Add this import to the top of your test file:
import org.mockito.ArgumentMatchersSugar._
Without this, the * wildcard might not be recognized, leading to verification failures even when the method was called with matching parameters.
3. Proxy Class Sanity Checks
You mentioned a proxy sits between your mock and test subject—while proxies usually just delegate calls (which Mockito tracks fine), a couple things to confirm:
- The proxy isn't skipping calls to the mock (e.g., caching results and not forwarding subsequent calls). Your interaction log shows calls are getting through, so this is less likely, but it's worth double-checking the proxy logic.
- The proxy is passing parameters through unchanged. Your debug logs confirm the method is called with correct parameters, so this is probably not the issue, but it's a good sanity check.
4. Clean Up Your Stub Syntax
Your stub's parameter types don't match the actual execute method signature from your proxy. The proxy calls service.execute(state: State, p: Parameter, ThirdParameter()), but your stub uses (f:Parameter, s:Parameter, t:Parameter). Fix the types in your answers function to match the real method:
mocked.execute(*, *, *) answers { (state: State, p: Parameter, third: ThirdParameter) => p match { case _:SecondParameter => Right("Second") case o => Left(o) } }
A type mismatch here could cause unexpected behavior, even if the method is called.
Corrected Test Example
Here's how your test should look with all these fixes applied:
import org.mockito.ArgumentMatchersSugar._ import org.mockito.MockitoSugar._ import scala.concurrent.Await import scala.concurrent.duration.Duration // Your Service, ServiceProxy, Subject definitions go here val mocked = mock[Service] mocked.init() returns mock[State] mocked.execute(*, *, *) answers { (state: State, p: Parameter, third: ThirdParameter) => p match { case _:SecondParameter => Right("Second") case o => Left(o) } } val subject = new Subject(new ServiceProxy(mocked)) val response = subject.doSomething("A", 1) Await.result(response, Duration.Inf) // Verify the CORRECT method! mocked.execute(*, *, *) wasCalled atLeastOnce
内容的提问来源于stack exchange,提问作者gervais.b

