Skip to content

Commit 29645fc

Browse files
tomekl007pedja4
authored andcommitted
BAEL-724 (eugenp#1422) Property testing with Javaslang
* BAEL-724 add javaslang test and property testing example * BAEL-724 make test more readable * BAEL-724 change missspelled word to the Remainder
1 parent 52c1e6a commit 29645fc

2 files changed

Lines changed: 78 additions & 0 deletions

File tree

‎javaslang/pom.xml‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,17 @@
2222
<artifactId>javaslang</artifactId>
2323
<version>2.1.0-alpha</version>
2424
</dependency>
25+
<dependency>
26+
<groupId>io.javaslang</groupId>
27+
<artifactId>javaslang-test</artifactId>
28+
<version>${javaslang.test.version}</version>
29+
</dependency>
2530
</dependencies>
2631

32+
<properties>
33+
<javaslang.test.version>2.0.5</javaslang.test.version>
34+
</properties>
35+
2736
<build>
2837
<plugins>
2938
<plugin>
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.baeldung.javaslang;
2+
3+
4+
import javaslang.CheckedFunction1;
5+
import javaslang.collection.Stream;
6+
import javaslang.test.Arbitrary;
7+
import javaslang.test.CheckResult;
8+
import javaslang.test.Property;
9+
import org.junit.Test;
10+
11+
public class PropertyBasedTest {
12+
13+
public Stream<String> stringsSupplier() {
14+
return Stream.from(0).map(i -> {
15+
boolean divByTwo = i % 2 == 0;
16+
boolean divByFive = i % 5 == 0;
17+
18+
if(divByFive && divByTwo){
19+
return "DividedByTwoAndFiveWithoutRemainder";
20+
}else if(divByFive){
21+
return "DividedByFiveWithoutRemainder";
22+
}else if(divByTwo){
23+
return "DividedByTwoWithoutRemainder";
24+
}
25+
return "";
26+
});
27+
}
28+
29+
@Test
30+
public void givenArbitrarySeq_whenCheckThatEverySecondElementIsEqualToString_thenTestPass() {
31+
//given
32+
Arbitrary<Integer> multiplesOf2 = Arbitrary.integer()
33+
.filter(i -> i > 0)
34+
.filter(i -> i % 2 == 0 && i % 5 != 0);
35+
36+
//when
37+
CheckedFunction1<Integer, Boolean> mustEquals =
38+
i -> stringsSupplier().get(i).equals("DividedByTwoWithoutRemainder");
39+
40+
41+
//then
42+
CheckResult result = Property
43+
.def("Every second element must equal to DividedByTwoWithoutRemainder")
44+
.forAll(multiplesOf2)
45+
.suchThat(mustEquals)
46+
.check(10_000, 100);
47+
48+
result.assertIsSatisfied();
49+
}
50+
51+
@Test
52+
public void givenArbitrarySeq_whenCheckThatEveryFifthElementIsEqualToString_thenTestPass() {
53+
//given
54+
Arbitrary<Integer> multiplesOf5 = Arbitrary.integer()
55+
.filter(i -> i > 0)
56+
.filter(i -> i % 5 == 0 && i % 2 == 0);
57+
58+
//when
59+
CheckedFunction1<Integer, Boolean> mustEquals = i ->
60+
stringsSupplier().get(i).endsWith("DividedByTwoAndFiveWithoutRemainder");
61+
62+
//then
63+
Property.def("Every fifth element must equal to DividedByTwoAndFiveWithoutRemainder")
64+
.forAll(multiplesOf5)
65+
.suchThat(mustEquals)
66+
.check(10_000, 1_000)
67+
.assertIsSatisfied();
68+
}
69+
}

0 commit comments

Comments
 (0)