I am trying jqwik (version 1.5.1) and I read from the documentation that I can create an Arbitrary
whose generated value depends on the one supplied by another Arbitrary
, specifically using the flatMap
function.
My actual goal is different, but based on this idea: I need 2 Arbitrary
s that always generate different values for a single test. This is what I tried:
@Provide
private Arbitrary<Tuple.Tuple2<Integer, Integer>> getValues() {
var firstArbitrary = Arbitraries.integers().between(1, Integer.MAX_VALUE);
var secondArbitrary = firstArbitrary.flatMap(first ->
Arbitraries.integers().between(1, Integer.MAX_VALUE).filter(i -> !i.equals(first)));
return Combinators.combine(firstArbitrary, secondArbitrary).as(Tuple::of);
}
@Property
public void test(@ForAll("getValues") Tuple.Tuple2<Integer, Integer> values) {
assertThat(values.get1()).isNotEqualTo(values.get2());
}
And it immediately fails with this sample:
Shrunk Sample (1 steps)
-----------------------
arg0: (1, 1)
Throwing an AssertionError
of course:
java.lang.AssertionError:
Expecting:
1
not to be equal to:
1
I expected the filter
function would have been enough to exclude the generated value produced by the firstArbitrary
but it seems like it is not even considered, or more likely it does something else. What am I missing? Is there an easier way to make sure that, given a certain number of integer
generators, they always produce different values?