gpt4 book ai didi

exception - 如何在 Scala 中使用 JUnit ExpectedException?

转载 作者:行者123 更新时间:2023-12-04 13:14:40 25 4
gpt4 key购买 nike

我希望能够使用 JUnit 4.7 的 ExpectedException @Rule在斯卡拉。但是,它似乎没有捕捉到任何东西:

import org.junit._

class ExceptionsHappen {

@Rule
def thrown = rules.ExpectedException.none

@Test
def badInt: Unit = {
thrown.expect(classOf[NumberFormatException])
Integer.parseInt("one")
}
}

这仍然失败, NumberFormatException .

最佳答案

要在 Scala 中使用 JUnit 4.11,您应该对注释进行元注释,以便注释仅应用于(合成)getter 方法,而不是基础字段:

import org.junit._
import scala.annotation.meta.getter

class ExceptionsHappen {

@(Rule @getter)
var thrown = rules.ExpectedException.none

@Test
def badInt: Unit = {
thrown.expect(classOf[NumberFormatException])
Integer.parseInt("one")
}
}

关于exception - 如何在 Scala 中使用 JUnit ExpectedException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7352087/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com