August 2018
Intermediate to advanced
524 pages
14h 45m
English
A bug is not trivial, and as usual, this is not in the implementation of the algorithm, but rather in the definition, or the lack of it. What should the program do if there are not only strings in the collection that we sort?
If I create a new test that starts with the following lines, it will throw ClassCastException:
@Test(expected = ClassCastException.class)
public void canNotSortMixedElements() {
var actualNames = new ArrayList(Arrays.asList(
42, "Wilson",
"Wilkinson", "Abraham", "Dagobert"
));
//... the rest of the code is the same as the previous test
The problem here is that Java collections can contain any type of element. You cannot ever be sure that a collection, such as ArrayList, contains only the ...