Is it possible to create a custom Arbitrary Generator in a ScalaTest (which mixins Checkers for ScalaCheck property) which is testing Java code? for e.g. following are the required steps for each test within forAll

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

above is a prep code before asserting results etc.

有帮助吗?

解决方案

You sure can. This should get you started.

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
  }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top