java - testing the order of a collection -
given list of objects i'd test return in correct order, not assert entire object.
for example i'd verify they're in order by
id 1, id 2, id 3,
or in case
date mostrecent date older date oldest
or in yet case
enum valuea enum valueb enum valuec
basically want test sort specified went through correctly single property on object affects this, i'd specify test variant of hasfirstitem( withpropertyequalto ... has seconditem( withpropertyequalto
i know can write
assertequals( property, list.get(0).id ) assertequals( property, list.get(1).id )
but i'd rather makes failure bit more obvious being sort issue, , perhaps declaratively, testing whole collection @ once. possible?
you should able use hamcrest's matcher hasproperty
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" name of property want check. note works methods named getxxx
aimed test javabeans.
Comments
Post a Comment