Skip to content

Commit

Permalink
Merge pull request #142 from PagerDuty/fix-async-npe
Browse files Browse the repository at this point in the history
Better exception when mock is invoked after expectations are verified.
  • Loading branch information
barkhorn authored Nov 30, 2016
2 parents 6aaf5c8 + 3c8112c commit d862ac5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
13 changes: 9 additions & 4 deletions core/src/main/scala/org/scalamock/function/FakeFunction.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,14 @@ class PathSpecTest extends path.FunSpec with Matchers with PathMockFactory {

}

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?")
}
}
}

0 comments on commit d862ac5

Please sign in to comment.