From 29645fc0d12fdade2cdc03104aa8a04873ca8505 Mon Sep 17 00:00:00 2001 From: Tomasz Lelek Date: Mon, 20 Mar 2017 12:18:41 +0100 Subject: [PATCH] BAEL-724 (#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 --- javaslang/pom.xml | 9 +++ .../baeldung/javaslang/PropertyBasedTest.java | 69 +++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 javaslang/src/test/java/com/baeldung/javaslang/PropertyBasedTest.java diff --git a/javaslang/pom.xml b/javaslang/pom.xml index e111132aec..7bb23c0daf 100644 --- a/javaslang/pom.xml +++ b/javaslang/pom.xml @@ -22,8 +22,17 @@ javaslang 2.1.0-alpha + + io.javaslang + javaslang-test + ${javaslang.test.version} + + + 2.0.5 + + diff --git a/javaslang/src/test/java/com/baeldung/javaslang/PropertyBasedTest.java b/javaslang/src/test/java/com/baeldung/javaslang/PropertyBasedTest.java new file mode 100644 index 0000000000..3acac34550 --- /dev/null +++ b/javaslang/src/test/java/com/baeldung/javaslang/PropertyBasedTest.java @@ -0,0 +1,69 @@ +package com.baeldung.javaslang; + + +import javaslang.CheckedFunction1; +import javaslang.collection.Stream; +import javaslang.test.Arbitrary; +import javaslang.test.CheckResult; +import javaslang.test.Property; +import org.junit.Test; + +public class PropertyBasedTest { + + public Stream stringsSupplier() { + return Stream.from(0).map(i -> { + boolean divByTwo = i % 2 == 0; + boolean divByFive = i % 5 == 0; + + if(divByFive && divByTwo){ + return "DividedByTwoAndFiveWithoutRemainder"; + }else if(divByFive){ + return "DividedByFiveWithoutRemainder"; + }else if(divByTwo){ + return "DividedByTwoWithoutRemainder"; + } + return ""; + }); + } + + @Test + public void givenArbitrarySeq_whenCheckThatEverySecondElementIsEqualToString_thenTestPass() { + //given + 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"); + + + //then + CheckResult result = Property + .def("Every second element must equal to DividedByTwoWithoutRemainder") + .forAll(multiplesOf2) + .suchThat(mustEquals) + .check(10_000, 100); + + result.assertIsSatisfied(); + } + + @Test + public void givenArbitrarySeq_whenCheckThatEveryFifthElementIsEqualToString_thenTestPass() { + //given + 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"); + + //then + Property.def("Every fifth element must equal to DividedByTwoAndFiveWithoutRemainder") + .forAll(multiplesOf5) + .suchThat(mustEquals) + .check(10_000, 1_000) + .assertIsSatisfied(); + } +}