lundi 2 novembre 2015

Checking values extracted from Collection against primitive value in AssertJ

I had this case where I needed that all the objects in a collection have a specific boolean value in a field. extracting() seemed like a very good candidate for this. I was able to 'access' the field pretty easily but when I wanted to check it againt the value I was not able to find a clean way to do it in the API. The first thing I came up was the following:

        SomeService someService = new SomeService();
        List<ClassA> llList = someService.getList();
        assertThat(llList).extracting("someBoolean")
                .are(new Condition<Object>() {
                    @Override
                    public boolean matches(Object o) {
                        return Boolean.FALSE.equals(o);
                    }
                });

This seems like it is such common thing to check that I am buffled that I could not find something better in the AssertJ API. To enhance readability I next did the the following:

assertThat(llList).extracting("someBoolean")
                .are(createBooleanCondition(false));

public static Condition<Object> createBooleanCondition(boolean expected){
       return new Condition<Object>() {
           @Override
           public boolean matches(Object o) {
               return new Boolean(expected).equals(o);
           }
       };
}

Is there a cleaner way to express this kind of assertion with the AssertJ?

NOTE: I could use a java8 lambda to make this cleaner but my question is geared towards assertj and I also have the hard rule that I need this to compile in java 1.7.

Aucun commentaire:

Enregistrer un commentaire