I'm trying to test a curried private method (actually function) using PrivateMethodTester
but can't to figure out how to invoke it
Consider following curried method:
object MyObject {
..
private def curriedAdder(augend: Int)(addend: Int): Int = augend + addend
..
}
Following gives compilation error
assert(9, MyObject.invokePrivate(PrivateMethod[Int]('curriedAdder)(2)(7)))
..
[error] /path/to/MyObject.scala:76:80: org.scalatest.PrivateMethodTester.Invocation[Int] does not take parameters
[error] assert(9, MyObject.invokePrivate(PrivateMethod[Int]('curriedAdder)(2)(7)))
[error] ^
..
While this one fails to find the function
assert(9, MyObject.invokePrivate(PrivateMethod[Int => Int]('curriedAdder)(2))(7))
..
[info] - my-test-name *** FAILED ***
[info] java.lang.IllegalArgumentException: Can't find a private method named: curriedAdder
[info] at org.scalatest.PrivateMethodTester$Invoker.invokePrivate(PrivateMethodTester.scala:247)
..
PrivateMethodTester
?Framework versions:
"org.scalatest" %% "scalatest" % "3.0.3" % Test
scalaVersion := "2.11.11"
sbt.version=1.0.3
The following worked for me with ScalaTest 3.0.1:
val curriedAdder = PrivateMethod[Int]('curriedAdder)
val actualResult = (MyObject invokePrivate curriedAdder(2, 7))
assert(9 == actualResult)
According to docs:
The private method is invoked dynamically via reflection
When I hear reflection I think of Object.getClass()
, so calling MyObject.getClass.getDeclaredMethods.foreach(println)
the method showed up as
private int example.HelloSpec$MyObject$.curriedAdder(int,int)
and so curriedAdder(int,int)
was a hint for me on how to call this curried method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With