From a90532946c6d75da2a91db8be7094be71661000e Mon Sep 17 00:00:00 2001 From: Graham Cox Date: Thu, 27 Jul 2023 09:53:04 +0100 Subject: [PATCH] BAEL-5847: Property-Based Testing with jqwik (#14489) --- testing-modules/jqwik/.gitignore | 1 + testing-modules/jqwik/pom.xml | 57 +++++++++++++++++++ .../com/baeldung/jqwik/AdditionLiveTest.java | 13 +++++ .../com/baeldung/jqwik/DivisionLiveTest.java | 45 +++++++++++++++ .../com/baeldung/jqwik/ShrinkingLiveTest.java | 15 +++++ testing-modules/pom.xml | 1 + 6 files changed, 132 insertions(+) create mode 100644 testing-modules/jqwik/.gitignore create mode 100644 testing-modules/jqwik/pom.xml create mode 100644 testing-modules/jqwik/src/test/java/com/baeldung/jqwik/AdditionLiveTest.java create mode 100644 testing-modules/jqwik/src/test/java/com/baeldung/jqwik/DivisionLiveTest.java create mode 100644 testing-modules/jqwik/src/test/java/com/baeldung/jqwik/ShrinkingLiveTest.java diff --git a/testing-modules/jqwik/.gitignore b/testing-modules/jqwik/.gitignore new file mode 100644 index 0000000000..9447111720 --- /dev/null +++ b/testing-modules/jqwik/.gitignore @@ -0,0 +1 @@ +/.jqwik-database diff --git a/testing-modules/jqwik/pom.xml b/testing-modules/jqwik/pom.xml new file mode 100644 index 0000000000..6ef9b61a6a --- /dev/null +++ b/testing-modules/jqwik/pom.xml @@ -0,0 +1,57 @@ + + + 4.0.0 + jqwik + 0.0.1-SNAPSHOT + jqwik + + + com.baeldung + testing-modules + 1.0.0-SNAPSHOT + + + + + net.jqwik + jqwik + 1.7.4 + test + + + org.junit.platform + junit-platform-engine + ${junit-platform.version} + test + + + org.junit.platform + junit-platform-console-standalone + ${junit-platform.version} + test + + + org.junit.jupiter + junit-jupiter-migrationsupport + ${junit-jupiter.version} + test + + + + + + + src/main/resources + true + + + src/test/resources + true + + + + + + diff --git a/testing-modules/jqwik/src/test/java/com/baeldung/jqwik/AdditionLiveTest.java b/testing-modules/jqwik/src/test/java/com/baeldung/jqwik/AdditionLiveTest.java new file mode 100644 index 0000000000..1099dd05ee --- /dev/null +++ b/testing-modules/jqwik/src/test/java/com/baeldung/jqwik/AdditionLiveTest.java @@ -0,0 +1,13 @@ +package com.baeldung.jqwik; + +import net.jqwik.api.ForAll; +import net.jqwik.api.Property; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class AdditionLiveTest { + @Property + public void additionIsCommutative(@ForAll int a, @ForAll int b) { + assertEquals(a + b, b + a); + } +} diff --git a/testing-modules/jqwik/src/test/java/com/baeldung/jqwik/DivisionLiveTest.java b/testing-modules/jqwik/src/test/java/com/baeldung/jqwik/DivisionLiveTest.java new file mode 100644 index 0000000000..f66a402e80 --- /dev/null +++ b/testing-modules/jqwik/src/test/java/com/baeldung/jqwik/DivisionLiveTest.java @@ -0,0 +1,45 @@ +package com.baeldung.jqwik; + +import net.jqwik.api.*; +import net.jqwik.api.constraints.Positive; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class DivisionLiveTest { + @Property + public void divideBySelf(@ForAll int value) { + int result = divide(value, value); + assertEquals(result, 1); + } + + @Property + public void dividePositiveBySelf(@ForAll @Positive int value) { + int result = divide(value, value); + assertEquals(result, 1); + } + + @Property + public void divideNonZeroBySelf(@ForAll("nonZeroNumbers") int value) { + int result = divide(value, value); + assertEquals(result, 1); + } + + @Property + public void divideLargeBySmall(@ForAll @Positive int a, @ForAll @Positive int b) { + Assume.that(a > b); + + int result = divide(a, b); + assertTrue(result >= 1); + } + + @Provide + Arbitrary nonZeroNumbers() { + return Arbitraries.integers() + .filter(v -> v != 0); + } + + private int divide(int a, int b) { + return a / b; + } +} diff --git a/testing-modules/jqwik/src/test/java/com/baeldung/jqwik/ShrinkingLiveTest.java b/testing-modules/jqwik/src/test/java/com/baeldung/jqwik/ShrinkingLiveTest.java new file mode 100644 index 0000000000..cd3396ae37 --- /dev/null +++ b/testing-modules/jqwik/src/test/java/com/baeldung/jqwik/ShrinkingLiveTest.java @@ -0,0 +1,15 @@ +package com.baeldung.jqwik; + +import net.jqwik.api.ForAll; +import net.jqwik.api.Property; +import net.jqwik.api.constraints.Positive; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class ShrinkingLiveTest { + @Property + public void square(@ForAll @Positive int a) { + int result = a * a; + assertTrue(result >= a); + } +} diff --git a/testing-modules/pom.xml b/testing-modules/pom.xml index 249be5c7c4..108ce29b86 100644 --- a/testing-modules/pom.xml +++ b/testing-modules/pom.xml @@ -23,6 +23,7 @@ groovy-spock hamcrest instancio + jqwik junit-4 junit-5-advanced junit-5-basics