java - Why when I unit test do I get coverage points with `final lazy val` but not `final val`? -
i have code (project source available here - https://github.com/natemurthy/testing-final-vals):
object main extends app { final val name = "foo" }
and i'm using scalatest , sbt coverage plugin test code so:
import org.scalatest.{flatspec,matchers} class maintest extends flatspec matchers { should "have name" in { main.name shouldbe "foo" } }
but reason coverage points when include lazy
modifier in expression:
why case?
my guess coverage tool counts executed lines.
final val name = "foo"
is compiled constant inline value in bytecode private static final
in java. when accessing variable read value bytecode constant. more info on inlining constant values during compiling
final lazy val name = "foo"
on other hand compiles lazy construction method since there no lazy values in jvm. if access variable lazy construction method executed. more info on scala lazy value bytecode generation
Comments
Post a Comment