A field inside an object that extends the application has a null value. Why is this so?

I am trying to use scalatest to test the application as follows:

object Main extends App {
    val name = "Greg"
}

class JunkTests extends FunSpec with MustMatchers {
    describe("Junk Tests") {
        it("Junk-1 -- Must do stuff") {
            println("Name: "+Main.name)
            // some test here
        }
    }
}

The output of my name is always zero. How can I get my main object to use its capabilities during the test? In actual use, I have an application that has an http server and I want to send it to messages, but now it has never been initialized, so the server never started. This simple example shows that Main is never initialized.

+6
source share
1 answer

, DelayedInit, , , .

Scaladoc

, :

  • define val final: final val name = "Greg"
  • : def name = "Greg"
  • lazy val: lazy val name = "Greg" ( , , , )
+10

All Articles