Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create custom Arbitrary generator for testing java code from ScalaTest ScalaCheck

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.

like image 785
user2066049 Avatar asked Dec 09 '22 09:12

user2066049


1 Answers

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
  }
}
like image 70
joescii Avatar answered Jan 21 '23 15:01

joescii