scala学习14之scalatest中的intercept使用

scalatest中的intercept中可以测试某种错误而顺利运行,比如:

 intercept[AssertionError] {
      target1.merge(target2)
    }

就可以检测merge是否有AssertionError,如果有则通过

AssertionError:
表达式 assert(condition) 将在condition条件不成立的时候抛出 AssertionError。

scalatest中代码:

 sparkTest("joining simple realignment targets on different chr throws exception") {
    val target1 = new IndelRealignmentTarget(Some(ReferenceRegion("1", 10, 16)),
      ReferenceRegion("1", 1, 21))
    val target2 = new IndelRealignmentTarget(Some(ReferenceRegion("2", 10, 16)),
      ReferenceRegion("2", 6, 26))

    intercept[AssertionError] {
      target1.merge(target2)
    }
  }

其中merge为:

  def merge(target: IndelRealignmentTarget): IndelRealignmentTarget = {
    assert(readRange.isAdjacent(target.readRange) || readRange.overlaps(target.readRange),
      "Targets do not overlap, and therefore cannot be merged.")

    val newVar = (variation, target.variation) match {
      case (Some(v), Some(tv)) => Some(v.hull(tv))
      case (Some(v), _) => Some(v)
      case (_, Some(tv)) => Some(tv)
      case _ => None
    }

    new IndelRealignmentTarget(newVar, readRange.merge(target.readRange))
  }

参考
【1】 http://blog.sina.com.cn/s/blog_a3869e9a01017dcn.html

你可能感兴趣的:(scala,scala)