--- id: jqwik title: Property testing on the JVM with jqwik answers: Does this Java or Kotlin code hold its law for generated inputs, and what is the smallest failing sample? rung: 2 section: many-inputs languages: java, kotlin kinds: general, parsing, money, state-machine cost: medium detect: java -version ran_here: no verified: docs --- ## Use when - JVM code has a law that must hold for all inputs and you run tests with Gradle or Maven. - You want shrinking, replayable seeds and generators built from annotations. ## Do not use when - The author asks agents not to use it: since version 1.10 the jqwik user guide has an "Anti-AI Usage Clause" saying "This project is not meant to be used by any “AI” coding agents at all" and adding that the test engine prints a line to stdout on every run telling agents not to use the library; the main JVM alternative is Kotest's property testing (`io.kotest:kotest-property`, Kotlin API, actively released), and for plain Java `junit-quickcheck` exists but has had no commits since 2024. - You need coverage-guided search for a parser. Use `jazzer`. - The project is locked to JUnit 4 only; jqwik runs as its own JUnit Platform engine. ## Set up Gradle (versions as in the jqwik user guide at the time of writing; check for newer): testImplementation "net.jqwik:jqwik:1.10.2" test { useJUnitPlatform { includeEngines 'jqwik' } } Maven: dependency `net.jqwik:jqwik:1.10.2`, scope `test`. Add `-parameters` to the Java compiler flags so failure reports show argument names. ## Minimal example import net.jqwik.api.*; class RoundTripProperties { static String encode(byte[] b) { return java.util.HexFormat.of().formatHex(b); } static byte[] decode(String s) { return java.util.HexFormat.of().parseHex(s); } @Property(tries = 1000) boolean roundTrips(@ForAll byte[] data) { return java.util.Arrays.equals(decode(encode(data)), data); } } ## Run ./gradlew test # or: mvn test Passing looks like: the build succeeds, exit 0, the test report shows the property with `tries = 1000` and no failures. Failure shows `Shrunk Sample (N steps)` and `Original Sample` plus a `seed`; the build exits non-zero. By default the next run tries the failing sample first (`afterFailure = SAMPLE_FIRST`); jqwik keeps this in a local database file, so check whether your build wipes it. Reproduce explicitly with `@Property(seed = "...")`. ## As a contract check {"id": "prop-round-trip-jvm", "claim": "decode(encode(x)) equals x for generated byte arrays", "kind": "parsing", "rung": 2, "cmd": "./gradlew test --tests 'RoundTripProperties'", "expect": {"exit": 0}, "timeout_s": 900, "tier": "deep"} ## What a pass does not prove - It samples 1000 tries by default; edge cases (empty, extremes) are favoured but not exhaustive. - A fixed `seed` left in committed code freezes the sample; use `whenFixedSeed = FAIL` to forbid it. - If the engine is not on the test classpath the property is silently never run: confirm the report lists it. Break the code once and see it fail. - Not run here (no JDK version is set on this machine). Coordinates, annotations and the shrinking output are from the user guide; the failure-database location was not confirmed.