DanMc DanMc - 4 months ago 27
Java Question

JUnit assert, value is between two integers

I need to write a JUnit test for an algorithm I wrote that outputs a random integer between two known values.

I need a JUnit test (I.e. an assertEquals like test) that asserts that the out-putted value is between these two integers (or not).

I.e. I have the values 5 and 10, the output would be a random value between 5 and 10. If the test is positive the number was between the two values, otherwise it was not.

Answer
@Test
public void randomTest(){
  int random = randomFunction();
  int high = 10;
  int low = 5;
  assertTrue("Error, random is too high", high >= random);
  assertTrue("Error, random is too low",  low  <= random);
  //System.out.println("Test passed: " + random + " is within " + high + " and + low);
}