Generating nullable objects in Scala
ScalaCheck has completely changed how I approach testing. In a recent problem, I needed to be able to easily generate objects that could be null. Here's how I approached it:
-
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.scalacheck.Gen | |
def couldBeNull[T >: Null](generator: Gen[T]): Gen[T] = for { | |
value <- Gen.oneOf(Gen.const(null), generator) | |
} yield value |
-
Comments