我有一个类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... */
}
所以,问题是:如何在不运行应用程序(主)的情况下初始化应用程序的成员?
是否有任何理由为什么选择扩展'App'来定义'main'方法:'def main(args:Array [String]):Unit = ...'?后面的方法没有这样的问题。 – Aivean
这似乎是做事的“斯卡拉方式”。我会心跳切换到Java方式,但是我想知道是否有一种不太可靠的方法来实现它...... –
实际上,这两种方法同样是“scala”。 'main'方法甚至在scala站点上的“hello,world”示例中使用:http://www.scala-lang.org/documentation/getting-started.html – Aivean