1

Currently I am doing this

while (permissions.hasNext()) {
  assertEquals(permissions.next().getRole(), "everybody");
}

This works fine, but is there a better way to do this ?

Asking this since Sonar is currently showing a violation, saying "Add at least one assertion to this test case." I believe it is not able to read the assert inside the loop. Is this a bug in Sonar?

GhostCat
  • 137,827
  • 25
  • 176
  • 248
Arpit
  • 323
  • 4
  • 13
  • If you would like to check collection has atleast one element, then you can do with isEmpty() check and also try retrieving element with it's index and check assert – Anil Kumar Athuluri Aug 01 '17 at 16:23
  • I guess that "Add at least one assertion to this test case." happens when there is nothing in the collection, so hasNext() returns false the first time and does not enter the code with the assertion – Hristo Vrigazov Aug 01 '17 at 18:58
  • @HristoVrigazov, that is not the case here. The hasNext() is returning true, but Sonar still complains. – Arpit Aug 02 '17 at 09:57

4 Answers4

4

You could use Java 8 streams:

assertTrue(permissions
.stream()
.allMatch(permission -> permission.getRole().equals("everybody")));
Hristo Vrigazov
  • 1,357
  • 2
  • 12
  • 20
  • That works well, and Sonar is happy with that too! I wonder though whether this is a bug in Sonar where it does not read the assert in the loop ? – Arpit Aug 02 '17 at 10:03
2

Another, very expressive solution (as usual) - assertThat + hamcrest matchers:

assertThat(yourCollection, everyItem(hasProperty("role", "everybody)));

( I personally dislike a little bit that reflection will be used to turn "role" into a call to getRole() ). See here for further reading.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • 1
    Yeah, rename `getRole` method, and this test escapes the refactoring :-/ – qlown Aug 01 '17 at 19:25
  • Yep ... I am surprised that hamcrest has nothing else to offer here ... but I am too tired today to do more research. Maybe tomorrow. – GhostCat Aug 01 '17 at 19:26
  • Thanks @GhostCat, that works. But as pointed out by both you and qlown, I'm a little wary of using this, since the test case might just be ignored if someone changes the property and doesn't figure out what is wrong with the test case. – Arpit Aug 02 '17 at 09:55
  • @GhostCat It is useful to know that though, can come in handy in other situations – Hristo Vrigazov Aug 02 '17 at 10:02
  • And @Arpit, I did a bit of research - couldnt find another "fully hamcrest" solution. Which is a bit of a shame, as I really like using assertThat(). – GhostCat Aug 02 '17 at 10:35
1

You can use contains, e.g.:

assertTrue(yourCollection.contains("permissions"));

Here's the Javadoc.

Darshan Mehta
  • 30,102
  • 11
  • 68
  • 102
  • How does that help with the question, and how come was this upvoted twice? – qlown Aug 01 '17 at 19:14
  • @qlown this is what the question reads : `How can I assert values in a collection` and that's what the answer explains? – Darshan Mehta Aug 01 '17 at 21:00
  • Title is vague, full question asks *how to assert that all values in a collection have a certain property*. Your answer shows *how to assert that the collection contains a certain value*, and that is asked nowhere, not even the title you quote asks that. – qlown Aug 02 '17 at 06:47
  • @qlown That is actually a better question, I'll just update the question to be less generic and vague. – Arpit Aug 02 '17 at 10:46
1

A solution using AssertJ:

assertThat(permissions).extracting("role").containsOnly("everybody");

or if you don't like reflection to extract the value, you can use stream:

assertThat(permissions.stream().map(Permission::getRole)).containsOnly("everybody");