0

I would like to test if the elements in a list are in a particular order. Specifically I would like to test for a member of the elements. So something like:

assertThat(listOfObjects).hasProperty(name).inOrder("one", "two", "three");

Is is possible to do something like this ? Right now I manually iterate over the elements and have an assertion for each one.

Saad Farooq
  • 13,172
  • 10
  • 68
  • 94

1 Answers1

0

Having a quick browse though the version 1 source code (link here) I found this method which claims to be doing what you want:

/**
 * Verifies that the actual {@code List} contains the given objects, in the same order. This method works just like
 * {@code isEqualTo(List)}, with the difference that internally the given array is converted to a {@code List}.
 *
 * @param objects the objects to look for.
 * @return this assertion object.
 * @throws AssertionError       if the actual {@code List} is {@code null}.
 * @throws NullPointerException if the given array is {@code null}.
 * @throws AssertionError       if the actual {@code List} does not contain the given objects.
 */
public @Nonnull ListAssert containsExactly(@Nonnull Object... objects) {
  checkNotNull(objects);
  return isNotNull().isEqualTo(newArrayList(objects));
}

If I'm understanding the Javadoc correctly, you would just do this:

assertThat(listOfObjects).containsExactly(object1, object2, object3);

Note though that there were some issues with this method in version 2.0-M8. As detailed here! They were resolved in version 2.0-M9.

Dan Temple
  • 2,736
  • 2
  • 22
  • 39
  • Yes. But the issue is I want to a check on a property i.e. the objects are not strings but have a property called `name` that has those values. – Saad Farooq Oct 26 '14 at 10:44
  • @SaadFarooq Ah, I'm with you now. I'm not sure you can do that with Fest. Certainly, I'm not able to see a method to retrieve a property from the ListAssert, or the extended classes. Perhaps raise a feature request with the FEST team? Is there any reason the objects would not be in order if the property was in the order you expected? It's not ideal, but I'm wondering if you could order them based on the property and then assert the objects were in the correct order? – Dan Temple Oct 26 '14 at 10:48
  • Well the ordering happens in the class that I am testing. The test is to see if a method does the ordering correctly. The class instances (objects) are complex. I can't recreate them in my test so I just test for one property. I'm sure I can write a matcher for this case but I was wondering if someone had already done so. – Saad Farooq Oct 26 '14 at 11:08