-1

I try to mock Arrays.sort methods to make sure the implementation in the QuickSort class doesn't make use of Arrays.sort. How can I do this? This is my try, which results in a java.lang.StackOverflowError

  @Test
  public void testQuickSort() {

    Integer[] array = {3, -7, 10, 3, 70, 20, 5, 1, 90, 410, -3, 7, -52};
    Integer[] sortedArray = {-52, -7, -3, 1, 3, 3, 5, 7, 10, 20, 70, 90, 410};
    QuickSort<Integer> quicksort = new QuickSort<>();

    new Expectations(Arrays.class) {{
        Arrays.sort((Integer[]) any);
    }};

    quicksort.quicksort(array);

    // some asserts
    // assertArrayEquals(sortedArray, array);
    // ...
  }
Sadık
  • 4,249
  • 7
  • 53
  • 89
  • 1
    why do you want to test this? – Tim Aug 02 '18 at 14:34
  • I want to test that the implementation of quicksort method is correct. That's done within a certain test framework. Not important for the question though... – Sadık Aug 02 '18 at 21:26

2 Answers2

0

You need mock it and limit the times calling Arrays.sort to 0:

@Test
public void testQuickSort(@Mocked Arrays mock) {
    new Expectations() {{
        mock.sort((Integer[]) any);
        times = 0;
    }};

    quicksort.quicksort(array);
}
xingbin
  • 27,410
  • 9
  • 53
  • 103
0

I was able to mock a static method this way:

new MockUp<Arrays>() {
    @Mock
    public void sort(Object[] o) {
         System.out.println("Oh no.");
    }       
};

Source

Sadık
  • 4,249
  • 7
  • 53
  • 89