Search code examples
javajunithamcrest

Hamcrest matcher for list of complex objects with each containing a list of complex objects


Sorry for the long title, but my problem is as follows;

I have these classes;

public class A {
    int a1;
    int a2;
    List<B> listOfB;
}

and

public class B {
    int b1;
    int b2;
    List<C> listOfC;
}

and

public class C {
    int c1;
    int c2;
}

If it were only for B to assert the list of C it has, I would use the following custom matcher; Matcher<Iterable<C>> cMatcher = Matchers.hasItems(allOf(hasProperty("c1", equalTo(c1)), hasProperty("c2", equalTo(c2))))

But how can I do the assertion from A? I want to use this C list matcher in a larger scope matcher that does the following;

Matchers.hasItems(allOf(hasProperty("b1", equalTo(b1)), hasProperty("b2", equalTo(b2)), hasProperty("listOfC", cMatcher)))

So in a way I wish to match a B in listOfB where has given b1, and b2 values, together with its listOfC containing a specific C with values c1 and c2.


Solution

  • Sorry to answer my own question, but the code I gave at the end was correct. There were some problems with the matcher constraints of the inner list C.

    So to match a list in a list;

    Matcher<Iterable<C>> cMatcher = Matchers.hasItems(allOf(hasProperty("c1", equalTo(c1)), hasProperty("c2", equalTo(c2))))

    Then use this in the higher scope matcher;

    Matchers.hasItems(allOf(hasProperty("b1", equalTo(b1)), hasProperty("b2", equalTo(b2)), hasProperty("listOfC", cMatcher)))

    will match for the case I have described in my question.

    The problem I have encountered is a field in my C class, which has type of Boolean, somehow hasProperty("boolField", true) does not match, saying property "boolField" is not readable. Probably due to the getter method of Boolean hasn't got a get prefix, in this question, it says primitive boolean works, while Boolean object fails in this situation