Reputation: 7394
I am trying to write a Junit
test to assert that Objects in my TreeMap
are in the correct order.
How can I do so?
I am trying to do the following:
assertEquals(treeMapFromMethod.get(0), ValueThatShouldBeFirstEntryInTreeMap);
But I am getting the error:
"java.lang.ClassCastException: java.lang.Integer cannot be cast to MyObJect"
This error relates to the Line above.
How can I fix this/ is there a better way to test this?
Upvotes: 0
Views: 2177
Reputation: 10511
If you use Hamcrest you can do this easily with the contains
matcher:
TreeMap<String, Integer> map = new TreeMap<>();
map.put("one", 1);
map.put("two", 2);
map.put("three", 3);
Assert.assertThat(map.keySet(), Matchers.contains("one", "three", "two"));
The contains
matcher checks, if the given collection has the same length and contains the given items in the same order.
Upvotes: 3