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;
}
In fact you have to assert more than that.
To check the actual behavior of the method, you should indeed assert from the returned list that :
Assert.assertEquals(size, list.size());
Assert.assertEquals(new HashSet<Long>(list).size(), actualList.size());
for (long v : list){
Assert.assertTrue(v " + " is not in the expected range", v >= min && v <= max);
}
As M. le Rutte underlined, I would also argue that the method API would make more sense to return a Set
instead of List
as an order of elements is not expected : the number are random.
And as a side note, your implementation is not efficient.
With a large range and a size requested close to the range size, you could loop much more than required.