BAEL-5847: Property-Based Testing with jqwik (#14489)

This commit is contained in:
Graham Cox 2023-07-27 09:53:04 +01:00 committed by GitHub
parent 4b08a40eae
commit a90532946c
6 changed files with 132 additions and 0 deletions

1
testing-modules/jqwik/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/.jqwik-database

View File

@ -0,0 +1,57 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>jqwik</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>jqwik</name>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>testing-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>net.jqwik</groupId>
<artifactId>jqwik</artifactId>
<version>1.7.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-engine</artifactId>
<version>${junit-platform.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-console-standalone</artifactId>
<version>${junit-platform.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-migrationsupport</artifactId>
<version>${junit-jupiter.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/test/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
</project>

View File

@ -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);
}
}

View File

@ -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<Integer> nonZeroNumbers() {
return Arbitraries.integers()
.filter(v -> v != 0);
}
private int divide(int a, int b) {
return a / b;
}
}

View File

@ -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);
}
}

View File

@ -23,6 +23,7 @@
<module>groovy-spock</module>
<module>hamcrest</module>
<module>instancio</module>
<module>jqwik</module>
<module>junit-4</module>
<module>junit-5-advanced</module>
<module>junit-5-basics</module>