Search code examples
javaassertj

AssertJ: how apply a comparison to any possible values without Condition


Just curious if is possible through AssertJ do something like this:

assertThat(somePojo).isNotNull()
                    .extracting("someproperty")
                    .doesNotContainNull()
                    .contains(any("a", "b"))

assertThat(somePojo).isNotNull()
                    .extracting("othersomeproperty")
                    .doesNotContainNull()
                    .contains(any(77, 88))

The any is a term or representation about the method that I need to use to check if the property value has any of the values provided, the someproperty for the case one could be either a or b, same thought for the second case but according with 77 or 88

The type to evaluate should be any, String, Integer etc..

If I use anyOf method seems the unique way is through Condition<T> according with:

Not sure (I did a do a research and not results) if is possible use other method and get the same anyOf behaviour but without Condition<T>


Solution

  • Try containsAnyOf which has been added in 3.9.0, note that extracting properties returns a list (of one element in your case since you are extracting only one property).

    Iterable<String> abc = Arrays.asList("a", "b", "c");
    
    // assertions will pass
    assertThat(abc).containsAnyOf("b")
                   .containsAnyOf("b", "c")
                   .containsAnyOf("a", "b", "c")
                   .containsAnyOf("a", "b", "c", "d")
                   .containsAnyOf("e", "f", "g", "b");
    

    Alternatively, use isIn to check that the value under test is in a given set of values.