2014-02-17 62 views
4

是否有可能在測試Java代碼的ScalaTest中創建自定義的任意生成器(混合了Checkers for ScalaCheck屬性)?例如,以下是對每一個測試中FORALL上述創建自定義的任意生成器來測試ScalaTest的Java代碼ScalaCheck

val fund = new Fund() 
val fundAccount = new Account(Account.RETIREMENT) 
val consumer = new Consumer("John") 
.createAccount(fundAccount) 
fund.addConsumer(consumer) 
fundAccount.deposit(amount) 

是之前斷言結果等

回答

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 
    } 
} 
相關問題