How to claim that each item in a collection is within a range

I have a Groovy array that will receive a given number of random Integer values. And I want to argue that every element in the array has a value within a given range. I am trying to use Hamcrest Matchers. So my test is as follows:

@Test void testShouldReturnArrayOfStats(){ def results = pg.rollStats() assertThat results, everyItem(both(greaterThan(0)).and(lessThanOrEqualTo(6))) } 

When I run the test, I get assertionError

 java.lang.AssertionError: Expected: every item is (a value greater than <0> and a value less than or equal to <6>) but: was [<6>, <3>, <5>, <4>, <3>, <2>] 

I have tried some variations of this, but I am not getting the passing test. just looking at the β€œBut: was” part of the error, I see that all 6 values ​​meet the requirements, but the test still doesn't work.

I have not used Groovy or Hamcrest for a very long time, so I'm sure something is missing. Thanks

+4
source share
1 answer

Could you use groovy?

 assert results.every { it in 1..6 } 
+4
source

Source: https://habr.com/ru/post/1445248/


All Articles