I have a simple faulty method that I would like to write a JUnit statement to test Here is the code:
public static ArrayList union(ArrayList a, ArrayList b) {
ArrayList d;
int randNum = (int) Math.random();
// if random is dividable by 2 then a is return else b will.
if (randNum % 2 == 0)
{
return a;
}
else
{
return b;
}
}
Here is the problem casting Math.random()
is not going to produce an integer random number. So, when the method is called it is going to return the array list a. I would like to write a test case that covers this statement and make it fail to show that It is not reaching b.
Currently I have this test case:
@Test
public void testUnion_2() throws Exception {
ArrayList a = new ArrayList();
ArrayList b = new ArrayList();
ArrayList result = SectionOne.union(a, b);
// add additional test code here
assertNotNull(result);
assertEquals(0, result.size());
}
Thanks I really appreciate the help.