Testing collection order

Given the list of objects that I would like to validate, they are returned in the correct order, but I would not want to assert the entire object.

For example, I would like to check that they are ok

id 1, 
id 2,
id 3,

      

or otherwise

date mostRecent
date older
date oldest

      

or in another case

enum ValueA
enum ValueB
enum ValueC

      

Basically, I want to check that the sorting type I specified went through correctly, but only one property of the object affects this, so I would like to specify my test with some variation hasFirstItem( withPropertyEqualTo ... has secondItem( withPropertyEqualTo

I know I can write

 assertEquals( property, list.get(0).id )
 assertEquals( property, list.get(1).id )

      

but I would rather do something that makes the failure more obvious in the matter of sorting and perhaps declaratively, checking the entire collection right away. Is it possible?

+3


source to share


5 answers


You should be able to use the hamcrest matcher hasProperty

like this:

public class Foo {

    private String a;

    public Foo(String a) {
        this.a = a;
    }

    public Object getStr() {
        return a;
    }


    public static void main(String[] args) {
        List<Foo> l = Arrays.asList(new Foo("a"), new Foo("b"));
        Assert.assertThat(l, contains(hasProperty("str", equalTo("a")),
                                      hasProperty("str", equalTo("b"))));
    }

}

      



where "str" ​​is the name of the property you want to check. Note that this only works for named methods getXxx

as it is for JavaBeans testing.

+3


source


One way to do this is to simply sort the list according to the given property, and then compare the sorted list with the original:



public class MyObjectIdComparator implements Comparator<MyObject> {

    @Override
    public int compare (MyObject a, MyObject b) {
        return a.getId().compareTo(b.getId());
    }
}

ArrayList<MyObject> orig = getListFromSomewhere();
ArrayList<MyObject> sorted = new ArrayList<>(orig);
Collections.sort (sorted, new MyObjectIdComparator());

assertEquals ("orig list is in the wrong order, sorted, orig);

      

+2


source


Can you iterate over the collection and report the first time the property of interest failed?

for (int i=0; i<list.size()-1; ++i) {
 if (list.get(i) > list.get(i+1)) {
    fail(String.format("%s > %s; in the wrong place in the sorted list for index %d",
        list.get(i), list.get(i+1), i));
 }
}

      

+1


source


You can specify the message in your assert command,

assertEquals("Sort out of order at index " + i, expected.get(i), list.get(i));

      

or,

assertSame("Sort out of order at index " + i, expected.get(i), list.get(i));

      

0


source


Assuming the property object implements the Comparable interface :

Object previous = list.get(0);

for (Object element : list) {
    assertTrue(previous.getProperty().compareTo(element.getProperty()) <= 0);
    previous = element;
}

      

0


source







All Articles