2: SortNumOfComparisons -> Sort_Comparisons
This commit is contained in:
parent
48197442a8
commit
29de1e37a5
@ -1,40 +0,0 @@
|
|||||||
package quality.software;
|
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.Comparator;
|
|
||||||
|
|
||||||
import static org.mockito.ArgumentMatchers.any;
|
|
||||||
import static org.mockito.Mockito.*;
|
|
||||||
|
|
||||||
public class X2_SortNumOfComparisons {
|
|
||||||
Integer[] array = {3, 2, 1};
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void mockingNotIdealHere() {
|
|
||||||
// Create a mocked Comparator.
|
|
||||||
// - Forward calls of Comparator.compare() to an actual comparator.
|
|
||||||
// - Record how many times compare() is called.
|
|
||||||
Comparator<Object> comparator = mock(Comparator.class);
|
|
||||||
when(comparator.compare(any(Integer.class), any(Integer.class)))
|
|
||||||
.thenAnswer(invocation ->
|
|
||||||
NaturalOrder.INSTANCE.compare(
|
|
||||||
invocation.getArgument(0, Integer.class),
|
|
||||||
invocation.getArgument(1, Integer.class)));
|
|
||||||
|
|
||||||
|
|
||||||
Arrays.sort(array, comparator);
|
|
||||||
|
|
||||||
verify(comparator, times(2)).compare(any(Integer.class), any(Integer.class));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void useSpyInstead(){
|
|
||||||
Comparator<Object> comparator = spy(NaturalOrder.INSTANCE);
|
|
||||||
|
|
||||||
Arrays.sort(array, comparator);
|
|
||||||
|
|
||||||
verify(comparator, times(2)).compare(any(), any());
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,93 @@
|
|||||||
|
package quality.software;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.mockito.ArgumentCaptor;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Comparator;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
|
import static org.mockito.Mockito.*;
|
||||||
|
|
||||||
|
public class X2_Sort_Comparisons {
|
||||||
|
Integer[] array = {3, 2, 1};
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void mockingNotIdealHere() {
|
||||||
|
// Create a mocked Comparator.
|
||||||
|
// - Forward calls of `Comparator.compare()` to an actual comparator.
|
||||||
|
// - Record how many times `compare()` is called.
|
||||||
|
Comparator<Object> comparator = mock(Comparator.class);
|
||||||
|
when(comparator.compare(any(Integer.class), any(Integer.class)))
|
||||||
|
.thenAnswer(invocation ->
|
||||||
|
NaturalOrder.INSTANCE.compare(
|
||||||
|
invocation.getArgument(0, Integer.class),
|
||||||
|
invocation.getArgument(1, Integer.class)));
|
||||||
|
|
||||||
|
|
||||||
|
Arrays.sort(array, comparator);
|
||||||
|
|
||||||
|
// Verify that `compare()` was called two times.
|
||||||
|
verify(comparator, times(2)).compare(any(Integer.class), any(Integer.class));
|
||||||
|
// Verify that nothing else was called.
|
||||||
|
verifyNoMoreInteractions(comparator);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void useSpyInstead() {
|
||||||
|
// Create a spied Comparator.
|
||||||
|
// - Record how many times `compare()` is called.
|
||||||
|
Comparator<Object> comparator = spy(NaturalOrder.INSTANCE);
|
||||||
|
|
||||||
|
Arrays.sort(array, comparator);
|
||||||
|
|
||||||
|
// Verify that `compare()` was called two times.
|
||||||
|
verify(comparator, times(2)).compare(any(Integer.class), any(Integer.class));
|
||||||
|
// Verify that nothing else was called.
|
||||||
|
verifyNoMoreInteractions(comparator);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void verifyArguments() {
|
||||||
|
Comparator<Object> comparator = spy(NaturalOrder.INSTANCE);
|
||||||
|
|
||||||
|
Arrays.sort(array, comparator);
|
||||||
|
|
||||||
|
// Verify that one `compare()` call was with arguments (`1`, `2`)
|
||||||
|
// and that the other one was with (`2`, `3`).
|
||||||
|
// -> We don't check the ordering of the `compare()` calls here!
|
||||||
|
verify(comparator, times(1)).compare(1, 2);
|
||||||
|
verify(comparator, times(1)).compare(2, 3);
|
||||||
|
// Verify that nothing else was called.
|
||||||
|
verifyNoMoreInteractions(comparator);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void verifyArgumentsAndTheirOrder() {
|
||||||
|
Comparator<Object> comparator = spy(NaturalOrder.INSTANCE);
|
||||||
|
|
||||||
|
Arrays.sort(array, comparator);
|
||||||
|
|
||||||
|
ArgumentCaptor<Integer> argCaptor1 = ArgumentCaptor.forClass(Integer.class);
|
||||||
|
ArgumentCaptor<Integer> argCaptor2 = ArgumentCaptor.forClass(Integer.class);
|
||||||
|
// Verify that `compare()` was called two times.
|
||||||
|
verify(comparator, times(2)).compare(argCaptor1.capture(), argCaptor2.capture());
|
||||||
|
// Verify that nothing else was called.
|
||||||
|
verifyNoMoreInteractions(comparator);
|
||||||
|
|
||||||
|
List<Integer> args1 = argCaptor1.getAllValues();
|
||||||
|
List<Integer> args2 = argCaptor2.getAllValues();
|
||||||
|
|
||||||
|
// Now the ordering of the two `compare()` calls matters!
|
||||||
|
|
||||||
|
// Verify the arguments passed in the first `compare()` call.
|
||||||
|
assertEquals(2, args1.getFirst());
|
||||||
|
assertEquals(3, args2.getFirst());
|
||||||
|
|
||||||
|
// Verify the arguments passed in the second `compare()` call.
|
||||||
|
assertEquals(1, args1.get(1));
|
||||||
|
assertEquals(2, args2.get(1));
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user