You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Using Scala 2, I tried testing a private method using PrivateMethodTester, which takes an AnyVal wrapper type.
Invocation fails with IllegalArgumentException("Can't find a private method named: "....
However, AnyVal types get erased.
Adding the following test case to PrivateMethodTesterSpec.scala should fail:
it("should work if the private method takes an AnyVal") {
final case class Wrapper(value: Int) extends AnyVal
class Modest {
private def secret(w: Wrapper) = w.value + 42
}
val secret = PrivateMethod[Int](Symbol("secret"))
assert(((new Modest) invokePrivate secret(Wrapper(1))) === 43)
}
Passing a value of the underlying type directly to the invocation, in this case secret(1), works.
IMHO, this is unexpected. I have no experience with Scala reflection and couldn't get something to work. Also, it seems like this has changed a lot from version 2 to 3.
@cptwunderlich Sorry for the really late response, somehow I missed this.
The PrivateMethodTester is implemented with java reflection, and at runtime there's no way we can determine that Wrapper is AnyVal, at JVM level the def secret takes an int as its argument but an instance Wrapper is being passed in. There is no precise way at runtime we can determine that Wrapper is an AnyVal and handle it accordingly.
One possible approach is we have do the check at compile time, there's a way to determine the whether a type being passed in is AnyVal like described here:
Using Scala 2, I tried testing a private method using PrivateMethodTester, which takes an AnyVal wrapper type.
Invocation fails with
IllegalArgumentException("Can't find a private method named: "...
.While debugging the issue, I found that Java reflection is used to get the argument types and then compared to the nominal argument types.
However,
AnyVal
types get erased.Adding the following test case to PrivateMethodTesterSpec.scala should fail:
Passing a value of the underlying type directly to the invocation, in this case
secret(1)
, works.IMHO, this is unexpected. I have no experience with Scala reflection and couldn't get something to work. Also, it seems like this has changed a lot from version 2 to 3.
scalatest version: 3.2.15
scala version: 2.13.10
java version: openjdk 11.0.17
The text was updated successfully, but these errors were encountered: