Reputation: 11888
Hey I am trying to test the following class :
class Foo {
def f: Int = 4 + g
def g: Int = 2
}
My test is the following:
class FooSpec extends PlaySpec with MockFactory {
val foo = new Foo()
"Foo" must {
"Call function f" in {
(foo.g _)
.expects()
.once()
.returns(5)
foo.f must be (9)
}
}
}
My test is failing saying that :
java.lang.NoSuchMethodException: Foo.mock$g$0()
java.lang.Class.getMethod(Unknown Source)
...
I am not sure to see why ...
I am using scalatest and scalamock :
"org.scalatestplus.play" %% "scalatestplus-play" % "1.5.0" % "test"
"org.scalamock" %% "scalamock-scalatest-support" % "3.2.2" % "test"
Upvotes: 4
Views: 1986
Reputation: 4798
A late answer but since this is the top google result for this error message, just adding another potential issue to look out for: make sure you aren't mocking a final
method.
This would result in the same error and scalamock doesn't allow that.
If you own the code, just un-final
the method or work around it in some other way that's acceptable for you.
Upvotes: 0
Reputation: 2900
I see two problems here:
val foo = new Foo()
, but you have to mock
this class first: val foo = mock[Foo]
g
and expect it to get called when you invoke f
- you'll have to restructurize your code in a way that Foo.g
is called from another class - wrap it in a delegate perhaps. Or use Mockito - it's not as fancy and does its magic in runtime as opposed to ScalaMock's compile time, but it provides an ability to callRealMethod()
on a mocked class.Basically ScalaMock works best when you mock
or stub
traits, not classes - their macros don't have to process actual implementations of the methods, and noone would expect them to.
Upvotes: 7