1

This test gives me all statuses as a Boolean value of either True or False from API:

List<Boolean> allStatus = event.getResponse().getBody().jsonPath().getList("enabled");

There is no specific idea of how many status there will be, and there is no fixed size; it could be size 20 or 1.

To check this assertion, I was running a for loop and checking each value:

assertNotNull(allStatus);
for (Boolean status : allStatus) {
    assertEquals("FAILED Disable event status ", false, status);
}

I want to know what is there a better way to handle such a scenario?

N..
  • 906
  • 7
  • 23

4 Answers4

3

You could use Java Streams's allMatch

assertNotNull(allStatus);
assertTrue(allStatus.stream().allMatch(b -> !b));
Jonah Graham
  • 7,890
  • 23
  • 55
  • 1
    Actually, you might want to revert to `b -> !b` because the [source code](http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/lang/Boolean.java#Boolean.equals%28java.lang.Object%29) for `equals` does unboxing also. Sorry about that – 4castle Jul 29 '16 at 14:43
0

You could use streams introduced by Java 8:

List<Boolean> allStatus = new ArrayList<>();
allStatus.stream().forEach((status) -> { assert status; });

This is basically doing the same as the code you provided, but with newer technology. You can't make it any faster because you do have to check every single entry.

Marco de Abreu
  • 663
  • 5
  • 17
0

Maybe do a single assertion?

boolean tmp = true;
for (Boolean status : allStatus) tmp = tmp&&status;
assert tmp;
evilpenguin
  • 5,448
  • 6
  • 41
  • 49
0

We can aslo use below assertion :

List<Boolean> allStatus = event.getResponse().getBody().jsonPath().getList("enabled");
assertNotNull(allStatus);
    assertEquals("FAILED Disable event status ", false, allStatus.contains(Boolean.valueOf(true)));
sauumum
  • 1,638
  • 1
  • 19
  • 36
  • I think this will check only once that status is there or not. In allStatus, It will be all status of different event – N.. Jul 29 '16 at 18:05