JUnit assert, value is between two integers

@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);
}

you can use junit assertThat method (since JUnit 4.4 )

see http://www.vogella.com/tutorials/Hamcrest/article.html

import static org.hamcrest.CoreMatchers.allOf;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.lessThan;
import static org.junit.Assert.assertThat;

......

@Test
public void randomTest(){
    int random = 8;
    int high = 10;
    int low = 5;
    assertThat(random, allOf(greaterThan(low), lessThan(high)));
}

Tags:

Java

Junit

Random