I'm trying to parse a simple Json using json4s, and am finding that it will work in the Main class of my program, but for some reason not in the unit test. Here's a minimal example:
build.sbt:
ThisBuild / scalaVersion := "2.13.2"
lazy val validatorbroken = (project in file("."))
.settings(
name := "validatorbroken",
)
libraryDependencies ++= Seq(
"org.json4s" %% "json4s-jackson" % "3.7.0-M4",
"org.scalatest" %% "scalatest" % "3.1.2" % "test",
)
the file containing the case class definition and the function:
package validatorbroken
import org.json4s._
import org.json4s.jackson.JsonMethods._
case class EasyInput(file: String, sheet: String)
object ProcessInput {
import Main.formats
def captureEasyInput(s: String): EasyInput = {
println(s"STRING JSON $s")
val rawJson = parse(s)
println(s"PARSED JSON $rawJson")
rawJson.extract[EasyInput]
}
}
The main file, which works fine:
package validatorbroken
import org.json4s._
import scala.io.Source
object Main extends App {
implicit val formats = DefaultFormats
val easyInputJson: String = Source.fromResource("easy_input.json").mkString
val capturedJson = ProcessInput.captureEasyInput(easyInputJson)
println(s"CAPTURED $capturedJson")
}
and the unit test, which does not:
package validatorbroken
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers
import scala.io.Source
class UnitTests extends AnyFlatSpec with Matchers {
implicit val formats = Main.formats
import ProcessInput._
"captureInput Function" should "obtain an instance of Input" in {
val easyInputJson: String = Source.fromResource("easy_input.json").mkString
val capturedJson = ProcessInput.captureEasyInput(easyInputJson)
println(s"CAPTURED $capturedJson")
}
}
I'm aware that there are some gotchas with having your case classes not at the top level of the package, but as far as I can see I've not stepped on that particular mine. Could anyone lend a hand? At the risk of sounding like an idiot if this proves to have a simple solution, this has already cost me a few hours of work.
My stack trace looks like this:
[info] - should obtain an instance of Input *** FAILED ***
[info] org.json4s.package$MappingException: unknown error
[info] at org.json4s.Extraction$.extract(Extraction.scala:46)
[info] at org.json4s.ExtractableJsonAstNode.extract(ExtractableJsonAstNode.scala:21)
[info] at validatorbroken.ProcessInput$.captureEasyInput(InputToItems.scala:19)
[info] at validatorbroken.UnitTests.$anonfun$new$1(Basic.scala:13)
[info] at scala.runtime.java8.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.scala:18)
[info] at org.scalatest.OutcomeOf.outcomeOf(OutcomeOf.scala:85)
[info] at org.scalatest.OutcomeOf.outcomeOf$(OutcomeOf.scala:83)
[info] at org.scalatest.OutcomeOf$.outcomeOf(OutcomeOf.scala:104)
[info] at org.scalatest.Transformer.apply(Transformer.scala:22)
[info] at org.scalatest.Transformer.apply(Transformer.scala:20)
[info] ...
[info] Cause: java.lang.NullPointerException:
[info] at org.json4s.Formats$.customDeserializer(Formats.scala:54)
[info] at org.json4s.Extraction$.customOrElse(Extraction.scala:662)
[info] at org.json4s.Extraction$.extract(Extraction.scala:410)
[info] at org.json4s.Extraction$.extract(Extraction.scala:42)
[info] at org.json4s.ExtractableJsonAstNode.extract(ExtractableJsonAstNode.scala:21)
[info] at validatorbroken.ProcessInput$.captureEasyInput(InputToItems.scala:19)
[info] at validatorbroken.UnitTests.$anonfun$new$1(Basic.scala:13)
[info] at scala.runtime.java8.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.scala:18)
[info] at org.scalatest.OutcomeOf.outcomeOf(OutcomeOf.scala:85)
[info] at org.scalatest.OutcomeOf.outcomeOf$(OutcomeOf.scala:83)
Thanks in advance for any help, and also afterwards.
...
Cause: java.lang.NullPointerException
...
This is a good indication of an order of initialization problem.
You use import Main.formats
in ProcessInput
and use ProcessInput
in Main
. I can't figure out exactly why it works in Main but not in the unit test, but I suggest moving the definition of formats
to ProcessInput
or making it an implicit argument to captureEasyInput
.