From 3c8112c0d474b4373e738dee63f271a6884b6021 Mon Sep 17 00:00:00 2001 From: David van Geest Date: Wed, 15 Jun 2016 15:11:18 -0400 Subject: [PATCH] Better error message/exception when mocks are called after verifyExpectations. --- .../scala/org/scalamock/function/FakeFunction.scala | 13 +++++++++---- .../org/scalamock/test/scalatest/PathSpecTest.scala | 10 ++++++++++ 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/core/src/main/scala/org/scalamock/function/FakeFunction.scala b/core/src/main/scala/org/scalamock/function/FakeFunction.scala index ca720c24..a0c19c90 100644 --- a/core/src/main/scala/org/scalamock/function/FakeFunction.scala +++ b/core/src/main/scala/org/scalamock/function/FakeFunction.scala @@ -37,9 +37,14 @@ abstract class FakeFunction(protected val mockContext: MockContext, private[scal private def expectationContext = mockContext.expectationContext def handle(arguments: Product): Any = { - val call = new Call(this, arguments) - callLog += call - expectationContext.handle(call) getOrElse onUnexpected(call) + if (callLog != null) { + val call = new Call(this, arguments) + callLog += call + expectationContext.handle(call) getOrElse onUnexpected(call) + } else { + val msg = "Can't log call to mock object, have expectations been verified already?" + throw new RuntimeException(msg) + } } protected def onUnexpected(call: Call): Any @@ -179,4 +184,4 @@ abstract class FakeFunction22[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, extends FakeFunction(mockContext, name) with Function22[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, R] with NiceToString { def apply(v1: T1, v2: T2, v3: T3, v4: T4, v5: T5, v6: T6, v7: T7, v8: T8, v9: T9, v10: T10, v11: T11, v12: T12, v13: T13, v14: T14, v15: T15, v16: T16, v17: T17, v18: T18, v19: T19, v20: T20, v21: T21, v22: T22) = handle((v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22)).asInstanceOf[R] -} \ No newline at end of file +} diff --git a/frameworks/scalatest/src/test/scala/org/scalamock/test/scalatest/PathSpecTest.scala b/frameworks/scalatest/src/test/scala/org/scalamock/test/scalatest/PathSpecTest.scala index 3cbd2917..792fd515 100644 --- a/frameworks/scalatest/src/test/scala/org/scalamock/test/scalatest/PathSpecTest.scala +++ b/frameworks/scalatest/src/test/scala/org/scalamock/test/scalatest/PathSpecTest.scala @@ -60,4 +60,14 @@ class PathSpecTest extends path.FunSpec with Matchers with PathMockFactory { testFailedException.getMessage() should include("mockFun(bottom-level) once (never called - UNSATISFIED)") } + describe("PathSpec") { + val mockFun = mockFunction[String, Unit] + + it("throws an exception with good error message if the mock is called after expectations are verified") { + verifyExpectations() + val thrown = the [RuntimeException] thrownBy(mockFun("called after verification")) + thrown should not be a[NullPointerException] + thrown.getMessage should include ("have expectations been verified already?") + } + } }