我有一个类MyClass
,它扩展了App
。问题是,在MyClass
中,有一个变量myValue
需要初始化,而无需实际运行应用程序。这是因为我想以非交互式的方式运行MyClass
方法的单元测试。
class MyClass extends App {
val myValue = "A value that I need to run the unit tests"
def myMethod: Unit = "A method that needs to be unit-tested and uses " + myValue
/* ... main (interactive) code that is not supposed to run in the unit test... */
}
所以,问题是:如何在不运行应用程序(main)的情况下初始化应用程序的成员?
来自App
特征的标量:
===注意事项===
需要注意的是,这个特性是使用
DelayedInit
功能实现的,这意味着对象的字段以前不会被初始化主方法已经执行。
似乎您唯一的选择是像在Java中那样声明主方法def main(args: Array[String]): Unit = ...
,而不是扩展App
。