Search code examples
javalisttestingassertj

comparing two list with different type in assertj


I write a converter for a list and I want to test it with assertj.

List<Bar> convert(List<Foo> input)

Imagine I know how to assert that a single Foo correctly converted to a Bar. My test is something like this

@Test
void test() {
    List<Foo> input = generateInput();

    List<Bar> actual = converter.convert(input);

    // TODO asserThat input list correctly coverted to output list
}

How can I assert that each element of actual corresponds to the element in same index of input and assert that correctly converted?
I'm looking for some method like containsExactlyElementsOf that get a custom satisfier to assert that Foo correctly converted to Bar.

One way is using a for loop like this but I'm looking for a better option:

assertThat(actual).hasSameSize(input);
for(int i = 0; i < actual.size(); i++) {
    Foo foo = input.get(i);
    Bar bar = actual.get(i);
    assertThatConvertedCorrectly(foo, bar);
}

Solution

  • One of my colleagues found a solution to this problem. There is an assertion zipSatisfy.
    Here is the description of the function.

    Verifies that the zipped pairs of actual and other elements, i.e.: (actual 1st element, other 1st element), (actual 2nd element, other 2nd element), ... all satisfy the given zipRequirements.
    This assertion assumes that actual and other have the same size but they can contain different type of elements making it handy to compare objects converted to another type, for example Domain and View/DTO objects.

    It does exactly what I was looking for.