I need to test a layer of a method that maps a list of objects into a list of another, eg transforming from a list of objects we don't control to a list of objects we do. I can't compare by using equals() since the objects are of different types, but I need to test that the list and object values are being mapped correctly.
My current implementation is:
public void test {
// GIVEN
List<T1> l1 = api.getData();
// WHEN
List<T2> l2 = getList();
// THEN
assertEquals(l1.size(), l1.size());
for(int i = 0; i < l1.size(); i++) {
assertEquals(l1.get(i).getVal1(), l2.get(i).getVal1());
assertEquals(l1.get(i).getVal2(), l2.get(i).getVal2());
assertEquals(l1.get(i).getVal3(), l2.get(i).getVal3());
...
}
}
I'm new to unit testing. Is there a better way to structure this test?
you can use assertj library like this:
import static org.assertj.core.api.Assertions.assertThat;
...
assertThat(l2).usingElementComparatorOnFields("val1", "va2", "val3").containsExactlyElementsOf(l1);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.