是否可以在ScalaTest(混合使用Checkers for ScalaCheck属性)中创建自定义任意生成器,该生成器正在测试Java代码?例如以下是forAll
中每项测试所需的步骤val fund = new Fund()
val fundAccount = new Account(Account.RETIREMENT)
val consumer = new Consumer("John")
.createAccount(fundAccount)
fund.addConsumer(consumer)
fundAccount.deposit(amount)
上面的是在断言结果等之前的准备代码。
答案 0 :(得分:14)
你确定可以。这应该可以帮到你。
import org.scalacheck._
import Arbitrary._
import Prop._
case class Consumer(name:String)
object ConsumerChecks extends Properties("Consumer") {
lazy val genConsumer:Gen[Consumer] = for {
name <- arbitrary[String]
} yield Consumer(name)
implicit lazy val arbConsumer:Arbitrary[Consumer] = Arbitrary(genConsumer)
property("some prop") = forAll { c:Consumer =>
// Check stuff
true
}
}