相关疑难解决方法(0)

Scala期望异常片段

我正在尝试模拟常见测试框架(例如JUnit或TestNG)的预期异常行为.

到目前为止,这是我能想到的(工作):

trait ExpectAsserts
{
  self : {
    def fail (message : String)
    def success (message : String)
  } =>

  def expect[T](exceptionClass : Class[T])(test : => Unit)
  {
    try
    {
      test
      fail("exception not thrown")
    }
    catch
    {
      case expected : T => success("got exception " + expected)
      case other : Exception => fail("expected "+ exceptionClass + " but " + other + " thrown instead.")
    }
  }
}

object Main extends ExpectAsserts
{
  def main (args : Array[String])
  {
    expect(classOf[ArithmeticException]) …
Run Code Online (Sandbox Code Playgroud)

scala pattern-matching type-erasure

0
推荐指数
1
解决办法
891
查看次数

标签 统计

pattern-matching ×1

scala ×1

type-erasure ×1