diff --git a/javaslang/pom.xml b/javaslang/pom.xml index 7bb23c0daf..941aac0802 100644 --- a/javaslang/pom.xml +++ b/javaslang/pom.xml @@ -38,7 +38,7 @@ org.apache.maven.plugins maven-compiler-plugin - 3.3 + 3.5.1 1.8 1.8 diff --git a/javaslang/src/test/java/com/baeldung/javaslang/PropertyBasedTest.java b/javaslang/src/test/java/com/baeldung/javaslang/PropertyBasedTest.java index 3acac34550..43f3d6e6a0 100644 --- a/javaslang/src/test/java/com/baeldung/javaslang/PropertyBasedTest.java +++ b/javaslang/src/test/java/com/baeldung/javaslang/PropertyBasedTest.java @@ -1,6 +1,5 @@ package com.baeldung.javaslang; - import javaslang.CheckedFunction1; import javaslang.collection.Stream; import javaslang.test.Arbitrary; @@ -8,42 +7,42 @@ import javaslang.test.CheckResult; import javaslang.test.Property; import org.junit.Test; +import java.util.function.Predicate; + +import static javaslang.API.*; + public class PropertyBasedTest { - public Stream stringsSupplier() { - return Stream.from(0).map(i -> { - boolean divByTwo = i % 2 == 0; - boolean divByFive = i % 5 == 0; + private static Predicate divisibleByTwo = i -> i % 2 == 0; + private static Predicate divisibleByFive = i -> i % 5 == 0; - if(divByFive && divByTwo){ - return "DividedByTwoAndFiveWithoutRemainder"; - }else if(divByFive){ - return "DividedByFiveWithoutRemainder"; - }else if(divByTwo){ - return "DividedByTwoWithoutRemainder"; - } - return ""; - }); + private Stream stringsSupplier() { + return Stream.from(0).map(i -> Match(i).of( + Case($(divisibleByFive.and(divisibleByTwo)), "DividedByTwoAndFiveWithoutRemainder"), + Case($(divisibleByFive), "DividedByFiveWithoutRemainder"), + Case($(divisibleByTwo), "DividedByTwoWithoutRemainder"), + Case($(), ""))); } @Test public void givenArbitrarySeq_whenCheckThatEverySecondElementIsEqualToString_thenTestPass() { //given - Arbitrary multiplesOf2 = Arbitrary.integer() - .filter(i -> i > 0) - .filter(i -> i % 2 == 0 && i % 5 != 0); + Arbitrary multiplesOf2 = Arbitrary + .integer() + .filter(i -> i > 0) + .filter(i -> i % 2 == 0 && i % 5 != 0); //when - CheckedFunction1 mustEquals = - i -> stringsSupplier().get(i).equals("DividedByTwoWithoutRemainder"); - + CheckedFunction1 mustEquals = i -> stringsSupplier() + .get(i) + .equals("DividedByTwoWithoutRemainder"); //then CheckResult result = Property - .def("Every second element must equal to DividedByTwoWithoutRemainder") - .forAll(multiplesOf2) - .suchThat(mustEquals) - .check(10_000, 100); + .def("Every second element must equal to DividedByTwoWithoutRemainder") + .forAll(multiplesOf2) + .suchThat(mustEquals) + .check(10_000, 100); result.assertIsSatisfied(); } @@ -51,19 +50,22 @@ public class PropertyBasedTest { @Test public void givenArbitrarySeq_whenCheckThatEveryFifthElementIsEqualToString_thenTestPass() { //given - Arbitrary multiplesOf5 = Arbitrary.integer() - .filter(i -> i > 0) - .filter(i -> i % 5 == 0 && i % 2 == 0); + Arbitrary multiplesOf5 = Arbitrary + .integer() + .filter(i -> i > 0) + .filter(i -> i % 5 == 0 && i % 2 == 0); //when - CheckedFunction1 mustEquals = i -> - stringsSupplier().get(i).endsWith("DividedByTwoAndFiveWithoutRemainder"); + CheckedFunction1 mustEquals = i -> stringsSupplier() + .get(i) + .endsWith("DividedByTwoAndFiveWithoutRemainder"); //then - Property.def("Every fifth element must equal to DividedByTwoAndFiveWithoutRemainder") - .forAll(multiplesOf5) - .suchThat(mustEquals) - .check(10_000, 1_000) - .assertIsSatisfied(); + Property + .def("Every fifth element must equal to DividedByTwoAndFiveWithoutRemainder") + .forAll(multiplesOf5) + .suchThat(mustEquals) + .check(10_000, 1_000) + .assertIsSatisfied(); } }