mardi 18 octobre 2016

how to test a method of swapping 2 elements in array using Junit

I have write 2 method in class which swaps 2 elements in array. but I am not sure how to test my methods in Junit, I want to test if the 2 element as correctly swapped, and to check if the index in array is valid. finally a test to check if method will swap strings as well as double. import java.util.Arrays;

public class GenericExample {

int index1;
int index2; 

public static <T> T [] swap(T[] array, int index1, int index2)
{
    T temp = array[index1];
    array [index1] = array [index2] ;
    array [index2] = temp;

    return array;

}

public static void main(String[] args) {
    Integer [ ] array = {1, 2, 3, 4};
    System.out.println("Array is: " + Arrays.toString(array));
    swap(array,1,3);
    System.out.println("Array is: " + Arrays.toString(array));
}

}

Aucun commentaire:

Enregistrer un commentaire