JavaHamcrest icon indicating copy to clipboard operation
JavaHamcrest copied to clipboard

Let Hamcrest test each POJO item of a collection (with dates) through a Tuple.

Open manueljordan opened this issue 7 years ago • 9 comments

Hello Hamcrest

For more details:

If in AssertJ is possible do the following:

.containsExactly(tuple("087", "Peter", "Jordani", parse("1980-01-01")),
                 ...
                 tuple("088", "Isaias", "Jordano", parse("1980-01-01")))

How would be possible do the same in Hamcrest?

  • .andExpect(model().attribute("personas", ???)

Even when I marked the solution how valid, it is currently verbose.

Add a Tuple approach in Hamcrest is valuable.

Thank you

manueljordan avatar Dec 23 '18 19:12 manueljordan

AssertJ has it's own implementation of Tuples. Although it would be nice, I'm not aware of anyone working on something like this for Hamcrest. I'm inclined to close this issue, unless someone wants to volunteer to write such an implementation. I'll leave this open for just a little bit longer, in case someone wants to comment further.

tumbarumba avatar Feb 02 '19 12:02 tumbarumba

I understand the situation. Thank you

manueljordan avatar Feb 03 '19 14:02 manueljordan

My solution

String[] properties = { "crossingDate", "time", "direction", "vehicle", "groupName" };
   assertThat(list,contains(
               hasProperties(properties, "24/04/2018", "14:24", "Southbound", "EY164FX", "130016451 "),
               hasProperties(properties, "24/04/2018", "10:55", "Northbound", "EY164FX", "130016451 ")
        ));

public static <T> Matcher<? super T> hasProperties(String[] properties, Object... values) {
    List<Matcher<? super T>> matchers = new ArrayList<>();
    for (int i = 0; i < properties.length; i++) {
      String property = properties[i];
      matchers.add(Matchers.hasProperty(property, equalTo(values[i])));
    }
    return Matchers.allOf(matchers);
  }

filippor avatar Mar 08 '19 16:03 filippor

Adding tuples is a bad idea. Tuples are weakly typed heterogeneous containers that make their contents accessible through poorly named members such as item1 and item2 etc.

@manueljordan Just create your own strongly typed objects that encapsulate the properties you want to match and then compare the two objects with the is() matcher. I am actually surprised that you don't already have a Person class for the elements of your collection.

nibsi avatar Oct 25 '19 09:10 nibsi

Hello @Nibsi

Yes, I have the Person class involved, but in testing is more clearer work with Tuples because you can see directly at a first glance what you are asserting/testing/comparing

manueljordan avatar Oct 25 '19 18:10 manueljordan

@manueljordan and you don't have a constructor or factory method in Person that does something similar to the tuple() method except it returns a Person?

nibsi avatar Oct 28 '19 12:10 nibsi

@Nibsi Like I said I want see the data to be compared directly:

tuple("087", "Peter", "Jordani", parse("1980-01-01")

Even when could be verbose until some point, is good for testing purposes.

manueljordan avatar Oct 28 '19 14:10 manueljordan

@manueljordan

assertThat(person, is(Person.of("087", "Peter", "Jordani", parse("1980-01-01"))));

nibsi avatar Oct 28 '19 15:10 nibsi

I see now, with Snippet code all go better, thanks!

Not sure if Hamcrest support that to be adapted to check collection's items.

manueljordan avatar Oct 30 '19 12:10 manueljordan