assertIterableEquals example

In this post under JUnit, I will show with example how to assert whether a expected collection has same elements, at the same position when compared to actual collection.

Pre JUnit 5, to do this,
1) we have to iterate item by item in both collection (expected and actual) at the same time
2) compare the items present at same position in both the collection
3) if they are equal we proceed to next element otherwise we fail the assert.

As show below

    assertEquals(testList.size(), outputList.size());
    for(Iterator<Integer> iterator1 = testList.iterator(), iterator2 = outputList.iterator(); iterator1.hasNext();) {
        int firstListValue = iterator1.next();
        int secondListValue = iterator2.next();
        if(firstListValue != secondListValue) {
            fail("two lists are not equal");
        }
    }

In JUnit 5 framework, we are already getting an assert method, provided out of the box to assert the contents of a collection.

This method is called “assertIterableEquals”.

For our example I have created a class which will generate multiplication table for a given number as shown below

MultiplicationTableGenerator

package package18;

import java.util.ArrayList;
import java.util.List;

public class MultiplicationTableGenerator {
    public List<Integer> generate(int tableNumber) {
        List<Integer> list = new ArrayList<>(0);
        for(int i = 0; i < 10; i++) {
            int result = tableNumber * (i + 1);
            list.add(i, result);
        }
        return list;
    }
}

In the above “generate” method, I am getting the number as a parameter, calculating the multiplication table, storing the results in a list and returning the list.

Below is the test class.

MultiplicationTableGeneratorTest

1  package package18;
2  
3  import org.junit.jupiter.api.Test;
4  
5  import java.util.ArrayList;
6  import java.util.List;
7  
8  import static org.junit.jupiter.api.Assertions.assertIterableEquals;
9  
10 public class MultiplicationTableGeneratorTest {
11     @Test
12     public void testGenerate() {
13         List<Integer> testList = new ArrayList<>(0);
14         testList.add(2);
15         testList.add(4);
16         testList.add(6);
17         testList.add(8);
18         testList.add(10);
19         testList.add(12);
20         testList.add(14);
21         testList.add(16);
22         testList.add(18);
23         testList.add(20);
24 
25         MultiplicationTableGenerator multiplicationTableGenerator = new MultiplicationTableGenerator();
26         List<Integer> outputList = multiplicationTableGenerator.generate(2);
27 
28         assertIterableEquals(testList, outputList);
29     }
30 }

In the above “testGenerate” method, I created a list “testList” which will have the expected elements.

The list contains results of multiplication table for number 2.

Then at line 25, I create an instance of “MultiplicationTableGenerator” and at line 26 I call “generate” method passing 2 as the argument. I get the actual list as return value of “generate” method.

Then at line 26, I call the “assertIterableEquals” method which takes two lists (i.e., testList, outputList) as argument.

Now this “assertIterableEquals” will verfy that second list (i.e., outputList) will have same elements as the first list (i.e., testList).

In this way we can assert a collection.

Leave a comment