Problems with matchers on Mockito spies

532 views
Skip to first unread message

Jeppe Nejsum Madsen

unread,
Nov 1, 2011, 6:41:33 AM11/1/11
to specs2...@googlegroups.com
Hi,

Maybe I'm expecting too much, but I can't seem to verify correctly on
Mockito spies.

I have this:

val newVehicle = spy(Vehicle.create)
println("*** new
v"+Integer.toHexString(System.identityHashCode(newVehicle)))

I then call the test methods and afterwards verify:

there was no(newVehicle).end
there was one(newVehicle).save

This results in:

java.lang.Exception: The mock was not called as expected:
Missing method call for verify(mock) here:
-> at org.specs2.mock.MockitoMocker$class.verify(MockitoMocker.scala:31)

Example of correct verification:
verify(mock).doSomething()

Also, this error might show up because you verify either of:
final/private/equals()/hashCode() methods.
Those methods *cannot* be stubbed/verified.

I've overidden the save method on Vehicle:

override def save = {
println("*** Saving v"+Integer.toHexString(System.identityHashCode(this)))
super.save
}

And I can see the method is in fact called:

*** new v 32935741
*** Saving v 32935741

Any hints?

/Jeppe

etorreborre

unread,
Nov 1, 2011, 7:05:03 AM11/1/11
to specs2...@googlegroups.com
Hi Jeppe,

Can you please be a bit more specific with the specs2 / mockito versions you are using?

If I try the following spec with specs2 1.7-SNAPSHOT and mockito-1.9.0-rc1 (i.e the latest one, but it is the same with 1.8.5):

import org.specs2._
import mock._
import matcher._  

class TestSpec extends mutable.Specification with Mockito { 

  "test" in {
    val v = spy(new Vehicle)
    //v.save
    there was no(v).end
    there was one(v).save
  }

}

trait Savable {
  def save = "save"
}

class Vehicle extends Savable {
  def end = "end"
  override def save = "saved"
}


Then I get the following failure:

[error]   The mock was not called as expected:
[error] Wanted but not invoked:
[error] vehicle.save();
[error] -> at examples.TestSpec$$anonfun$1$$anonfun$apply$2.apply(TestSpec.scala:13)
[error] Actually, there were zero interactions with this mock.
[error]  (TestSpec.scala:13)

Two things also:
  1. the error message you get is typical of a final method being mocked/verified somewhere. Maybe there is a generated final method that you're using somewhere?
  2. just downgrade to raw Mockito API in your example to be sure that specs2 "sugar" is not creating issues
Finally, if you have a fully executable example that would help me a lot in debugging :-)

Thanks,

Eric.

PS: off to bed now so I'll check the rest with you tomorrow,...


Jeppe Nejsum Madsen

unread,
Nov 1, 2011, 11:10:19 AM11/1/11
to specs2...@googlegroups.com
Thanks,

Updating both specs2 & Mockito to SNAPSHOT/RC1 didn't help. It seems
to be a Mockito problem verifying calls on methods defined in traits:


trait Foo {
def func = "ooh"
}

class Bar extends Foo {
def g = "aah"
}
val b = org.mockito.Mockito.spy(new Bar)

org.mockito.Mockito.verify(b, org.mockito.Mockito.times(0)).g
org.mockito.Mockito.verify(b, org.mockito.Mockito.times(0)).func

The first verify works, the second fails.

This is rather bad. Any good workarounds? Do you know of any work
being done in this area or should I bring this to the mockito folks?

/Jeppe

etorreborre

unread,
Nov 1, 2011, 5:42:06 PM11/1/11
to specs2...@googlegroups.com
I played a bit with that code this morning and it feels like a Mockito bug to me.

The code you have is very sensitive to the exact statements you're making. For example, if you don't check "g" things will be ok.
I also checked that this behavior was independent of mocking or spying.

Another check I did is replacing the "trait" with "abstract class". In that case the test is also ok.

What I suspect is that, at some point, Mockito checks the method "func" not on the Bar class but the static "func" method on the abstract Foo class (if you looked at the generated bytecode for traits you know what I mean).

So, yes, this one is worth asking on the Mockito mailing-list.

Eric.

Jeppe Nejsum Madsen

unread,
Nov 2, 2011, 4:52:07 AM11/2/11
to specs2...@googlegroups.com

I've added this http://code.google.com/p/mockito/issues/detail?id=287

But it seems Scala is not high on the priority list. I'm not that
familiar with the bytecode of Scala, so might be a while :-(

/Jeppe

Reply all
Reply to author
Forward
0 new messages