0

I raised a fake issue on mockito-scala : https://github.com/mockito/mockito-scala/issues/191 But I am still stuck with my problem.

So basically, I have stubbed answers for one method. I am sure that the stubbed method is executed with the correct parameters (debug mode enter in it):

val mocked = mock[Service]
mocked.execute(*, *, *) answers {(f:Parameter, s:Parameter, t:Parameter) =>
   s match {
     case _:SecondParameter => // Debug mode enter here
       Right("Second")
     case o => Left(o)
   }
})

Then i verify with a very broad verification : mocked.method(*, *, *) wasCalled atLeastOnce but my test is failing:

Wanted but not invoked:
mocked.method(
    <any>,
    <any>,
    <any>
);
-> at **hidden**

However, there were exactly 4 interactions with this mock:
mocked.init();
-> at **hidden**

mocked.method(
    FirstParameter,
    SecondParameter,
    ThirdParameter
);
-> at **hidden**

mocked.method(
    FirstParameter,
    OtherParameter,
    ThirdParameter
);
-> at **hidden**

mocked.method(
    FirstParameter,
    YetAnotherParameter,
    ThirdParameter
);
-> at **hidden**

Edit 1: There is a class between the mock[Service] and the test subject. Can it be the cause of this issue ?

class ServiceProxy(service: Service) {
  def execute(p:Parameter) = Future {
    val state = service.init()
    service.execute(state, p, new ThirdParameter())
  }
}

class Subject(proxy:ServiceProxy) {
  def doSomething(s:String, n:Int) = {
    val parameter = createParameter(s, n)
    proxy.execute(parameter)
  }
}

// In my test case 
val mocked = mock[Service]
mocked.init() returns mock[State]
mocked.execute(*, *, *) answers // Same as above
val subject = new Subject(new ServiceProxy(mocked))

val response = subject.doSomething("A", 1)
Await.result(response, Duration.Inf)

mocked.method(*, *, *) wasCalled atLeastOnce
gervais.b
  • 2,294
  • 2
  • 22
  • 46
  • Could you post a bit more code? I don't manage to reproduce the issue with what we have here. As a side note, in the past I've seen errors like this one when the params do not implement `equals()` correctly, altough using the `*` matcher it shouldn't matter... – ultrasecr.eth Jan 10 '20 at 10:13
  • Well, the code is not public. I try to mimic it in this question. But I will try to create a sample project to reproduce it.. – gervais.b Jan 10 '20 at 10:23
  • I don't think the intermediate layer can be the cause, it doesn't really matter who calls the mock and you can see it is being called as per the exception thrown. I'm sure I can quickly hunt it down if you put some code in github that can reproduce the issue. – ultrasecr.eth Jan 10 '20 at 15:08

0 Answers0