When I want to test whether a returned collection is empty, I found no elegant solution.
assertTrue(service.getSomeEntries(params).isEmpty())
This approach works, the downside being that it loses any information of the data.
assertEquals(new LinkedHashSet<>(service.getSth()), Collections.emptySet())
This approach kinda works, it doesn't even change the order. But there is some boilerplate. The way the assertion works, it only shows that the size differs (not perfect, but good enough).
Is there any really elegant solution to this problem? Of course, when the collection is supposed to actually contain more than one element and the order is not guaranteed, sets are required. Other than that, I would expect a nicer solution to be there.
Aucun commentaire:
Enregistrer un commentaire