Mic*_*ker 15 annotations scala junit4
我想使用Scala为JUnit 4测试设置一个预期的异常.我目前正在做类似以下的事情:
@Test(expected=classOf[NullPointerException])
def someTest() = {
// Some test code
}
Run Code Online (Sandbox Code Playgroud)
但是我得到以下编译器错误:
error: wrong number of arguments for constructor Test: ()org.junit.Test
Run Code Online (Sandbox Code Playgroud)
Jay*_*rod 16
这有点期待,但2.8中注释的语法已经改变为与您最初发布的相同.Tristan发布的语法在当前稳定版本中是正确的,但是当我将项目升级到夜间2.8编译器时,它给了我错误.我猜这是由于包含了命名和默认参数.Scala邮件列表上也有一些讨论.引用Lukas Rytz:
另请注意,在2.8.0中,java注释的语法将不再使用名称 - 值对,而是使用命名参数,即
Run Code Online (Sandbox Code Playgroud)@ann{ val x = 1, val y = 2} ==> @ann(x = 1, y = 2)
Tri*_*cek 10
scala处理属性的方式有点时髦.我认为你要做的事情应该表达如下:
@Test { val expected = classOf[ NullPointerException] }
def someTest {
// test code
}
Run Code Online (Sandbox Code Playgroud)
这对我有用(JUnit 4.10,Scala 2.10.2):
@Test(expected = classOf[NullPointerException])
def testFoo() {
foo(null)
}
Run Code Online (Sandbox Code Playgroud)
与Tristan 建议的类似,但此语法实际上可以在我的项目中编译并运行。
编辑:呃,仔细一看,这正是最初的问题。好吧,我想在答案中包含最新的工作语法也没什么坏处。