I'm testing a function called generateRandomByteInRange
.
It fails and I want to see the exact value it gets (and not only that it was less than -127).
I tried to surround the assert with try - catch and when it fails to print to the console but nothing happened.
Function -
byte generateRandomByteInRange(int minVal,int maxVal) {
Random rnd;
rnd = new Random();
return (byte) (rnd.nextInt(maxVal - minVal + 1) + minVal);
}
Test -
@Test
public void generateRandomByteInRange() {
int res;
int minVal,maxVal;
minVal = 1;
maxVal = 3;
for (int i = 0; i < 100; i++) {
res = AF.generateRandomByteInRange(minVal, maxVal);
assertThat(res, is(both(greaterThanOrEqualTo(minVal)).and(lessThanOrEqualTo(maxVal))));
}
minVal = -127;
maxVal = 128;
for (int i = 0; i < 1000; i++) {
res = AF.generateRandomByteInRange(minVal, maxVal);
try {
assertThat(res, is(both(greaterThanOrEqualTo(minVal)).and(lessThanOrEqualTo(maxVal))));
} catch (Exception e) {
System.out.println(res);
}
}
}
The test fails and I get the following message (in the right)