Using this method that generates an ArrayList using random values from a called method getRandomInt. What would I assert to create a test method that tests that the ArrayList doesn't contain duplicates?
public static int getRandom(int min, int max) {
return random.nextInt((max - min) + 1) + min;
}
public static ArrayList<Integer> getRandomIntegers(int size, int min, int max) {
ArrayList<Integer> number = new ArrayList<Integer>();
while (number.size() < size) {
int random = getRandom(min, max);
if(!number.contains(random)) {
number.add(random);
}
}
return number;
}
Using assertJ this is straight forward common-assertions-for-java-collections-in-assertj
@Test
public void noDuplicatesTest(){
List<Integer> numbers = Lists.newArrayList(1, 52, 12, 39, 45, 98, 100, 565, 6, 13);
assertThat(numbers).doesNotHaveDuplicates();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With