Skip to content

Fix verify macro to support mocks created inside fixture classes #214

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

Merged
merged 2 commits into from
Mar 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 16 additions & 13 deletions macro/src/main/scala/org/mockito/VerifyMacro.scala
Original file line number Diff line number Diff line change
Expand Up @@ -90,31 +90,34 @@ private[mockito] trait VerificationMacroTransformer {
} else throw new Exception(s"Couldn't recognize invocation ${show(invocation)}")
}

protected def transformMockWasNeverCalled[T: c.WeakTypeTag, R](c: blackbox.Context)(obj: c.Tree, called: c.Tree): c.Tree = {
import c.universe._
called match {
case q"$_.called" => q"verification(_root_.org.mockito.MockitoSugar.verifyZeroInteractions($obj))"
case q"$_.calledAgain" => q"verification(_root_.org.mockito.MockitoSugar.verifyNoMoreInteractions($obj))"
case q"$_.calledAgain.apply($_.ignoringStubs)" =>
q"verification(_root_.org.mockito.MockitoSugar.verifyNoMoreInteractions(_root_.org.mockito.MockitoSugar.ignoreStubs($obj): _*))"
}
}

protected def transformVerification[T: c.WeakTypeTag, R](c: blackbox.Context)(called: c.Tree): c.Tree = {
import c.universe._

called match {
case q"$_.VerifyingOps[$_]($invocation).was($_.called)($order)" =>
transformInvocation(c)(invocation, order, q"_root_.org.mockito.VerifyMacro.Once")

case q"$_.VerifyingOps[$_]($_.this.$obj).wasNever($called)($_)" =>
called match {
case q"$_.called" => q"verification(_root_.org.mockito.MockitoSugar.verifyZeroInteractions($obj))"
case q"$_.calledAgain" => q"verification(_root_.org.mockito.MockitoSugar.verifyNoMoreInteractions($obj))"
case q"$_.calledAgain.apply($_.ignoringStubs)" =>
q"verification(_root_.org.mockito.MockitoSugar.verifyNoMoreInteractions(_root_.org.mockito.MockitoSugar.ignoreStubs($obj): _*))"
}
case q"$_.VerifyingOps[$_]($a.$b).wasNever($called)($order)" =>
q"""
if (_root_.org.mockito.MockitoSugar.mockingDetails($a).isMock) ${transformInvocation(c)(q"$a.$b", order, q"_root_.org.mockito.VerifyMacro.Never")}
else ${transformMockWasNeverCalled(c)(q"$a.$b", called)}
"""

case q"$_.VerifyingOps[$_]($obj.$method[..$targs](...$args)).wasNever($_.called)($order)" =>
transformInvocation(c)(q"$obj.$method[..$targs](...$args)", order, q"_root_.org.mockito.VerifyMacro.Never")

case q"$_.VerifyingOps[$_]($obj).wasNever($called)($_)" =>
called match {
case q"$_.called" => q"verification(_root_.org.mockito.MockitoSugar.verifyZeroInteractions($obj))"
case q"$_.calledAgain" => q"verification(_root_.org.mockito.MockitoSugar.verifyNoMoreInteractions($obj))"
case q"$_.calledAgain.apply($_.ignoringStubs)" =>
q"verification(_root_.org.mockito.MockitoSugar.verifyNoMoreInteractions(_root_.org.mockito.MockitoSugar.ignoreStubs($obj): _*))"
}
transformMockWasNeverCalled(c)(obj, called)

case q"$_.VerifyingOps[$_]($invocation).wasCalled($times)($order)" =>
transformInvocation(c)(invocation, order, times)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package user.org.mockito.scalatest

import org.mockito.exceptions.verification.{ NeverWantedButInvoked, NoInteractionsWanted }
import org.mockito.scalatest.IdiomaticMockito
import org.scalatest.matchers.should.Matchers
import org.scalatest.{ fixture, Outcome }

class IdiomaticMockitoFixtureClassTest extends fixture.FlatSpec with IdiomaticMockito with Matchers {
class Foo {
def bar(a: String) = "bar"
def baz = "baz"
}

class FixtureParam {
val foo: Foo = mock[Foo]
}

def withFixture(test: OneArgTest): Outcome = {
val theFixture = new FixtureParam
withFixture(test.toNoArgTest(theFixture))
}

"Mockito" should "verifyNoMoreInteractions fixture objects" in { f: FixtureParam =>
"mocked" willBe returned by f.foo.bar("pepe")

f.foo wasNever called

f.foo.bar("pepe") shouldBe "mocked"

a[NoInteractionsWanted] should be thrownBy {
f.foo wasNever called
}
}

"Mockito" should "verify no calls on fixture objects methods" in { f: FixtureParam =>
"mocked" willBe returned by f.foo.bar("pepe")
"mocked" willBe returned by f.foo.baz

f.foo.bar("pepe") shouldBe "mocked"
f.foo.baz shouldBe "mocked"

a[NeverWantedButInvoked] should be thrownBy {
f.foo.bar(*) wasNever called
f.foo.baz wasNever called
}
}
}