1.斷言
assert(conditon)將在條件不成立的時候,拋出assertionError
assert(conditon,explanation)講在條件不成立的時候,拋出explanation作為說明
package com.scala.first
/**
* Created by common on 17-4-19.
*/
object Assert {
def main(args: Array[String]): Unit = {
val a = new Assert()
a.above1(0)
}
}
class Assert {
val value = 1
def above(that: Int): Unit = {
val thatVal = that
val thisVal = this.value
//如果條件不滿足,Exception in thread "main" java.lang.AssertionError: assertion failed
assert(thatVal == thisVal)
}
//另一種斷言
//如果條件不滿足,Exception in thread "main" java.lang.AssertionError: assertion failed
def above1(that: Int): Unit = {
{
val thatVal = that
val thisVal = this.value
} ensuring(that == this.value)
}
}
2.單元測試
Scala中提供了多種單元測試的方法,比如ScalaTest
ScalaTest提供了多種單元測試的方法,最簡單的就是創建org.scalatest.suite類,並在這些類中定義測試方法
如果cmd+shift+T的快捷鍵無效的話,在需要測試的類上右鍵,Go to Test,創建一個測試類
