Meowzen Meowzen - 6 months ago 37
Java Question

Java: Junit tests and ArrayList

I just started out with Java in school and have trouble with Junit tests. We were supposed to write a method that checks if a given arraylist is divisable by a given divisor. if the array is empty or the divisor is 0 it's supposed to return false. this is my code:

public class ArrayCheck {
/**
* Tests all elements of the given array,
* if they are divisible by the given divisor.
*
* @param arr
* array to be tested
* @param divisor
* number by which all elements of the given array should be divisible
* @return true if all elements are divisible by divisor
*/
public boolean allDivisibleBy(ArrayList<Integer> arr, int divisor) {
//check if number is dividable or if number is 0
for (int i=0; i<arr.size(); i++){
if (arr.get(i) % divisor==0){
return true;
}
else if(arr.get(i)==0){
return true;
}
//check if divisor is 0
else if (divisor==0){
return false;
}
}
return false;
}
}


And here is my Junit-test so far. I understand, that i need to create different arraylists to test out my method, however i don't seem to fully understand how to implement these arrays and test them:

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.util.ArrayList;
import java.util.Arrays;

import org.junit.Before;
import org.junit.Test;


public class ArrayCheckTest {

public ArrayCheck ArrayCheck = new ArrayCheck();

ArrayList<Integer> array1=new ArrayList<Integer>();
array1.add();



@Test(timeout = 1000)
public void testAllDivisibleBy() {

ArrayCheck.allDivisibleBy(array1, 2);
}
}


Any help in understanding Junit-test-implementation better would be appreciated! thanks!

Answer

To test the complete method, your test class should be like as follows. You have to cover all the condition in your testing method. So you have to write several test methods to cover the testing method.

Corrected code:

public class ArrayCheck {
    /**
     * Tests all elements of the given array, if they are divisible by the given
     * divisor.
     *
     * @param arr
     *            array to be tested
     * @param divisor
     *            number by which all elements of the given array should be
     *            divisible
     * @return true if all elements are divisible by divisor
     */
    public boolean allDivisibleBy(ArrayList<Integer> arr, int divisor) {
        // check if number is dividable or if number is 0
        for (int i = 0; i < arr.size(); i++) {
            if (arr.get(i) != 0) {
                if (divisor == 0)
                    return false;
                else if (arr.get(i) % divisor != 0)
                    return false;
            }
        }
        return true;
    }
}

Test Class for corrected code:

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.util.ArrayList;

import org.junit.Before;
import org.junit.Test;

public class ArrayCheckTest {

    public ArrayCheck ArrayCheck = new ArrayCheck();
    ArrayList<Integer> array1;
    ArrayList<Integer> array2;

    @Before
    public void beforeTest() {
        array1 = new ArrayList<Integer>();
        array1.add(2);
        array1.add(4);
        array2 = new ArrayList<Integer>();
        array2.add(0);
    }

    @Test(timeout = 1000)
    public void testAllDivisibleBy() {
        // This only the check part of the code
        assertTrue(ArrayCheck.allDivisibleBy(array1, 2));
    }

    @Test(timeout = 1000)
    public void testAllNotDivisibleBy() {
        // This only the check part of the code
        assertFalse(ArrayCheck.allDivisibleBy(array1, 3));
    }

    @Test(timeout = 1000)
    public void testZeroDivisible() {
        // This only the check part of the code
        assertFalse(ArrayCheck.allDivisibleBy(array1, 0));
    }

    @Test(timeout = 1000)
    public void testZeroElement() {
        // This only the check part of the code
        assertTrue(ArrayCheck.allDivisibleBy(array2, 2));
    }
}
Comments