I am attempting to understand and impliment blackbox / whitebox JUnit techniques on a SelectionSort class, but Im having trouble understanding what direction to take..
One of my failed attempts I have below..I try to test the size of the Array from my SelectionSort class, but my Method (unsortedArray) is not recognized..
@Test
public void testUnsortedArray() {
int n = 20;
int[] x = new int[n];
for (int i = 0; i < 20; i++) {
x[i] = (n);
n--;
i++;
}
SelectionSort2 a = new SelectionSort2();
assertArrayEquals(20, a.unsortedArray(x, 20));
}
Below is my SelectionSort class that was provided. Any help or guidance is very appreciated :)
public class SelectionSort2 {
public static void main(String[] args)
{
int n = 20;
int[] numArray = unsortedArray(n); // re-initialize
printArray(numArray,n);
selectSort2(numArray, n);
printArray(numArray, n);
}
//Generate data for the selection sort array
public static int[] unsortedArray(int n) {
int[] a = new int[n];
for (int index = 0; index < n; index++) {
a[index] = (n - index);
}
return a;
}
//print the array
public static void printArray(int[] data, int n)
{
for (int index = 0; index < n; index++) {
System.out.print(data[index] + " ");
}
System.out.println();
}
public static void selectSort2(int[] data, int n)
{
for (int numUnsorted = n; numUnsorted > 0; numUnsorted--) {
int maxIndex = 0;
for (int index = 1; index < numUnsorted; index++) {
if (data[maxIndex] < data[index])
maxIndex = index;
//swap the element
int temp = data[numUnsorted-1];
data[numUnsorted-1] = data[maxIndex];
data[maxIndex] = temp;
}
}
}
}
Black-box testing can be kind of envisioned as input-output pairs. You give your program a set of inputs and see whether the outputs match what you expect.
So in this case, you'd have something like:
input: {5, 3, 1}; expected output: {1, 3, 5}
input: {9, 7, 5, 6, 8, 34, 3, 6}; expected output: {3, 5, 6, 6, 7, 8, 9, 34}
input: {} expected output: {}
input: {1, 3, 5} expected output: {1, 3, 5}
and you would use something like assertArrayEquals()
to check that the program's output is what you expect it to be.
White-box testing is a bit more involved, because you're designing tests that attempt to exercise all possible paths through the code, which mean that white box tests tend to be a bit more implementation-specific. To be honest, I'm not very familiar with white-box testing, so there isn't much I can help you with. I'm guessing that white-box testing for this would be essentially looking at your code and looking for the various corner cases that might pop up during execution. Your code does seem to be pretty straightforward though so I'm not sure what cases you might come up that wouldn't already be covered by black-box tests...
For the specific test you gave, I believe the issue is in this line:
assertArrayEquals(20, a.unsortedArray(x, 20));
assertArrayEquals()
either takes two arrays as arguments or a String
and two arrays, with the String
acting as the error message. I don't think your code would compile, as neither of the arguments you pass it are valid. In addition, you don't appear to define an unsortedArray(int[], int)
method... Did you mean to do selectSort2(x, 20)
?
Once you fix that line the JUnit test should work. Commenting out that one line allowed the JUnit test to run on my computer at least.
One more thing -- you said you wanted to test the size of the array in the SelectionSort class. For that, assertTrue()
might be the method to use, but I'm not sure such a test would be useful, because array sizes cannot be changed and you are not returning a new array at any point.