mercredi 27 mai 2015

Creating a JUnit for an array with a number less than 20 in the array

Assignment: Write a JUnit test assuming you have an array of int values and you only want the JUnit test to fail if any of the values are less than 20.

I know it only asks for the JUnit assuming the other methods are already created. But I want to create them anyway. However I do not know how to go about it. This is my code so far:

package ArrayJU;

public class ArrayJUTest {

    public static void main(String[] args){
        ArrayJUTest array = new ArrayJUTest();
        int arr[] = {23,25,50,68,3};
        System.out.println(array.arrayLessThan(arr));
    }

    public boolean arrayLessThan(int array[]){
        for (int element : array) {

            if(element>20){
                return true;
            }
            else{
                return false;
            }

        }


    }

}

For the arrayLessThan Eclipse is telling me that I need to return a boolean, however I wouldn't know how to iterate through the array without a for loop. And if I return a true or a false outside the for loop it will defeat the purpose of what I'm trying to do with the if/else statements. How do I go about this? Your help will be greatly appreciated.

Aucun commentaire:

Enregistrer un commentaire