I'm initializing a companion object for one of my scala test suites. One of the fields in this companion object is lazy evaluated and uses some of the fields in the test suite for initialization. Something like:
class SomeClassSpec extends WordSpec with Matchers with OneInstancePerTest {
lazy val someFieldTheCompanionObjectNeeds = "some_field_I_need"
"my test class" should {
"do something interesting" when {
"I tell it to" in {
//a bunch of test code using the SomeTestClassCompanionObject.someConfigurationINeed field.
}
}
}
}
object SomeTestClassCompanionObject extends SomeClassSpec {
lazy val someConfigurationINeed = Config(SomeTestClass.someFieldTheCompanionObjectNeeds)
}
Don't ask. I know this is bad practice, but it has to be done, and this is largely unrelated to my question.
What I noticed here was, my SomeTestClassCompanionObject.someConfigurationINeed
field was not initialized if I tried to use it inside the when
block of the test, however it is initialized inside the in
block. My question is: what actually differentiates each of the should
, when
, in
scopes in Wordspec? I was under the impression that these were simply logical differentiations, but this test shows that different things are initialized at different times in the underlying "static" block of the JVM code.
Does anyone have any further reading or links to the Wordspec documentation that explains what's going on here?