How can I test a private method that takes a generic type using privateMethodTester in scala?
Let's say I have the following method:
private def parseValueForJsonKeyWithReturnType[A: TypeTag](
node: JsonNode,
key: String,
defaultValue: Option[A] = None): A = {
val parsedValue = Option(node.get(key)).map(value => {
typeOf[A] match {
case t if t =:= typeOf[String] =>
value.textValue()
case t if t =:= typeOf[Double] =>
value.asDouble()
case t if t =:= typeOf[Long] =>
value.asLong()
case _ => throw new RuntimeException(s"Doesn't support conversion to [type=${typeOf[A]}] for [key=${key}]")
}
})
parsedValue.getOrElse(defaultValue.get).asInstanceOf[A]
}
I can call the method like this
parseValueForJsonKeyWithReturnType[Boolean](jsonNode, key="hours")
parseValueForJsonKeyWithReturnType[String](jsonNode, key="hours")
parseValueForJsonKeyWithReturnType[Long](jsonNode, key="hours")
In the test, I'm trying to do
val parseValueForJsonKeyWithReturnTypeInt = PrivateMethod[Int]('parseValueForJsonKeyWithReturnType)
a[RuntimeException] shouldBe thrownBy (object invokePrivate parseValueForJsonKeyWithReturnType[Int](jsonNode, "total" , None))
to make sure it will throw Runtime exception for unsupported type
But I get this error:
error: value parseValueForJsonKeyWithReturnType of type SerializerTest.this.PrivateMethod[Int] does not take type parameters.
If i try without type parameters, build succeeds but i get illegalArgumentException
Expected exception java.lang.RuntimeException to be thrown, but java.lang.IllegalArgumentException was thrown.
Cause: java.lang.IllegalArgumentException: Can't find a private method named: parseValueForJsonKeyWithReturnType
What am I doing wrong? Probably the syntax is wrong.
object invokePrivate calculateTotalWithReturnType(jsonNode, "total" , None)
– Joinder"Cause: java.lang.IllegalArgumentException: Can't find a private method named: calculateTotalWithReturnType"
– Sisterhood