Refactor the test

Now that we have discussed what a good unit test is, let's improve our test. The first thing is to move the supporting classes to separate files. We will create ArrayListSortableCollection:

package packt.java9.by.example.ch03.bubble; 

import packt.java9.by.example.ch03.SortableCollection;

import java.util.ArrayList;

public class ArrayListSortableCollection implements SortableCollection {
final private ArrayList actualNames;

ArrayListSortableCollection(ArrayList actualNames) {
this.actualNames = actualNames;
}

@Override
public Object get(int i) {
return actualNames.get(i);
}

@Override
public int size() {
return actualNames.size();
}
}

This class encapsulates ArrayList and then implements the get and size methods to ArrayList access. ArrayList itself is declared as final. Recall that a final field has to be defined by the time the constructor finishes. This guarantees that the field is there when we start to use the object and that it does not change during the object lifetime. Note, however, that the content of the object, in this case, the elements of ArrayList, may change. If it were not the case, we would not be able to sort it.

The next class is StringComparator. This is so simple that I will not list it here; I will leave it to you to implement the java.util.Comparator interface that can compare two Strings. It should not be difficult, especially as this class was already a part of the previous version of the BubbleSortTest class (hint: it was an anonymous class that we stored in the variable named stringCompare).

We also have to implement ArrayListSwapper, which also should not be a big surprise.

package packt.java9.by.example.ch03.bubble; 

import packt.java9.by.example.ch03.Swapper;

import java.util.ArrayList;

public class ArrayListSwapper implements Swapper {
final private ArrayList actualNames;

ArrayListSwapper(ArrayList actualNames) {
this.actualNames = actualNames;
}

@Override
public void swap(int i, int j) {
Object tmp = actualNames.get(i);
actualNames.set(i, actualNames.get(j));
actualNames.set(j, tmp);
}
}

Finally, our test will look this:

package packt.java9.by.example.ch03.bubble; 

// ... imports deleted from print ...
public class BubbleSortTest {
@Test
public void canSortStrings() {
ArrayList actualNames = new ArrayList(Arrays.asList(
"Johnson", "Wilson",
"Wilkinson", "Abraham", "Dagobert"
));
ArrayList expectedResult = new ArrayList(Arrays.asList(
"Abraham", "Dagobert",
"Johnson", "Wilkinson", "Wilson"
));
SortableCollection names =
new ArrayListSortableCollection(actualNames);
Sort sort = new BubbleSort();
sort.setComparator(
new StringComparator());
sort.setSwapper(
new ArrayListSwapper(actualNames));
sort.sort(names);
Assert.assertEquals(expectedResult, actualNames);
}
}

Now this is already a test that can be understood in 15 seconds. It documents well how to use a sort implementation that we defined. It still works and does not reveal any bug, as I promised.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.158.134