如何设置一个预期的异常使用拉斯卡拉和JUnit4
-
20-08-2019 - |
题
我想设置一个预期的例外JUnit4测试使用卡拉.我现在做的事情相似的如下:
@Test(expected=classOf[NullPointerException])
def someTest() = {
// Some test code
}
但我得到以下编译错误:
error: wrong number of arguments for constructor Test: ()org.junit.Test
解决方案
一路斯的交易的属性是一个小时髦。我想什么你想要做的应该是表达这样的:
@Test { val expected = classOf[ NullPointerException] }
def someTest {
// test code
}
其他提示
您也可以尝试规格有:
class mySpec extends SpecificationWithJUnit {
"this expects an exception" in {
myCode must throwA[NullPointerException]
}
}
埃里克。
使用ScalaTest和JUnit一起,你可以这样做:
import org.scalatest.junit.JUnitSuite
import org.scalatest.junit.ShouldMatchersForJUnit
import org.junit.Test
class ExampleSuite extends JUnitSuite with ShouldMatchersForJUnit {
@Test def toTest() {
evaluating { "yo".charAt(-1) } should produce [StringIndexOutOfBoundsException]
}
}
这对我的作品(JUnit的4.10,斯卡拉2.10.2):
@Test(expected = classOf[NullPointerException])
def testFoo() {
foo(null)
}
什么特里斯坦建议类似,但这种语法实际上编译并在我的项目工作。
编辑:嗯,仔细一看,这正是原来的问题有哪些。嗯,我想拥有最新的工作语法也回答不受到伤害。
不隶属于 StackOverflow