0

For a Spring Framework App, about Testing:

If in AssertJ is possible apply the following for a collection:

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

What could be the best equivalent approximation of these three methods:

  • containsExactly
  • tuple
  • parse

To be applied in:

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

I did a research in google:

  • about collections there are samples for simple collections (String, Integer with hasItems)
  • about dates, is working only with Date objects, of course same type, but not with String too.
Manuel Jordan
  • 15,253
  • 21
  • 95
  • 158

1 Answers1

2

Please take a look on these tests examples:

public class Test {
        private List<Person> personList;
        private Person peter = new Person("087", "Peter", parse("1980-01-01"));
        private Person john = new Person("081", "John", parse("1980-01-22"));

        @BeforeEach
        void setup() {
            personList = new ArrayList<>();
            personList.add(peter);
            personList.add(john);
        }

        @Test
        void assertjTest() {
            assertThat(personList).extracting("age", "name", "date")
                    .containsExactly(
                            tuple("087", "Peter", parse("1980-01-01")),
                            tuple("081", "John", parse("1980-01-22"))
                    );
        }

        @Test
        void hamcrestTest() {
            org.hamcrest.MatcherAssert.assertThat(personList,
                    contains(
                            allOf(
                                    hasProperty("age", is("087")),
                                    hasProperty("name", is("Peter")),
                                    hasProperty("date", is(parse("1980-01-01")))
                            ),
                            allOf(
                                    hasProperty("age", is("081")),
                                    hasProperty("name", is("John")),
                                    hasProperty("date", is(parse("1980-01-22")))
                            )
                    ));

        }
}

And let's review in details:

containsExactly

contains method is an alternative for it. Whereas hasItem is more like assertj.contains.

tuple

May be replaced by combination of allOf and hasProperty. In my opinion it looks ugly and I would think about just using new Person("a","b","c"). Unless you have some extra fields that you do not want to verify.

parse

Here I've just used same method for both of them. If you will take a more precise look on it, you will notice that this method has nothing to do with matchers. It is just parses string to date using new SimpleDateFormat("yyyy-MM-dd"). If you don't want to use assertj's you could easily extract it to some util class.

Ivan Lymar
  • 2,180
  • 11
  • 26
  • Thank u. It works. About (1) `Whereas hasItem is more like assertj.contains` could you add a **new** `@Test` method about that?, I am not sure to understand your point. (2) `In my opinion it looks ugly and I would think about just using new Person("a","b","c")` Even if is ugly (I am agree, is verbose) is for presentation purposes to understand what have been returned, **but** could you add a **new** `@Test` method about that?. (3) I understand, just curious, where appears `new SimpleDateFormat("yyyy-MM-dd")`? I did do `Ctrl + Click` starting in the `parse` method, I couldn't find that sentence – Manuel Jordan May 07 '18 at 12:58
  • @ManuelJordan 1) to understand it better you may want to try it by yourself. Create a list with TWO persons and try to run contains and hasItem with ONE person. Results will be different. 2) Instead of comparing exact fields, you could compare entire objects, like contains(new Person(1,2,3,), new Person(2,4,5)).3) You should take a more precise look, everything is there:D – Ivan Lymar May 07 '18 at 13:40