How do we do a For-Each Assertion?

You can use a method

public boolean check(int... values) {
    for (int value : values) 
        if(!Within(value, x, y)) return false;
    return true;
}

assert check(values);

Another approach is to test for assertion if you have lots of checks

boolean assertEnabled = false;
assert assertEnabled = true;
if (assertEnabled) {
   // do lots of checks
}

Just create a method checking all the elements of the array :

assert allWithin(values, x, y);

...

private boolean allWithin(int[] values, int x, int y) {
    for (int value : values) {
        if (!within(value, x, y)) {
            return false;
        }
    }
    return true;
}

Tags:

Java

Assert