dimanche 6 août 2017

Testing a method

I'm stuck on this simple testing method for some reason it's failing and I can't figure out why. Seems like my assertEquals isn't correct. Can anyone see why?

Here is main class:

public class Main {
    static List<Integer> numbers;

    public static void main(String[] args) throws Exception {

        numbers = new ArrayList<Integer>() {
            {
                add(3);
                add(4);
                add(6);
                add(1);
                add(9);
            }

        };
        int x = 6;
        int result = searchNum(x, 0);
        if (result != -1) {
            System.out.println("There is " + x  + " in the array, and the index is: " + result + " in the list.");
        } else
            System.out.println("X is not found in List");
    }

    public static int searchNum(int x, Integer index) {

        if (index == numbers.size()){
            return -1;          
        }
        if (!(numbers.get(index) == x)) {
            return searchNum(x, index + 1);
        }

        return index;
    } 

}

Here is what I tried see my test class:

public class MainTest {

    @Test
    public void searchNumReturnsIndex1(){
        CS102DZ14 instance = new CS102DZ14();        

        int x = 6;
        int result = instance.searchNum(x, 2);

        assertEquals(1, instance.searchNum(x, 2));  
    }
}

Aucun commentaire:

Enregistrer un commentaire