vendredi 21 octobre 2016

How to test swap method in junit class for array of elements

I am using a generic swap method in one of my classes. Here's the following class.

import java.util.Arrays;

public class Week3Generic {

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));
}

} }

I have create the following test class in order to test that the swap method works. here is the following code:

public class GenericTest extends Generic {

@Test
public void swaping2index() {
    String input [] = { "1" ,"2" ,"3" };
    String expectedOutput [] = { "2" ,"1" ,"3" };           
    assertArrayEquals(swap(input, 0, 2), (expectedOutput));
}

private void assertArrayEquals(String[] swap, String[] strings) {
    // TODO Auto-generated method stub

}

}

My question is that: are they any other way to testing the swap method in order to ensure that my swap method works in junit Testing. Sorry of any inconvenience I am very new to java programming.

Aucun commentaire:

Enregistrer un commentaire