0

I have a below code.

public static Pageable defaultSort(Pageable currentPageable, String defaultSort) {
        return defaultSort(currentPageable, defaultSort, Sort.Direction.ASC);
    }

    public static Pageable defaultSort(Pageable currentPageable, String defaultSort, Sort.Direction defaultDir) {
        if(currentPageable.getSort().isSorted()) {
            return currentPageable;
        } else {
            return PageRequest.of(currentPageable.getPageNumber(), currentPageable.getPageSize(), new Sort(defaultDir, defaultSort));
        }
    }

And I am looking to mock it. I used below, but it gives -

org.mockito.exceptions.misusing.MissingMethodInvocationException: 
when() requires an argument which has to be 'a method call on a mock'.
For example:
    when(mock.getArticles()).thenReturn(articles);

Also, this error might show up because:
1. you stub either of: final/private/equals()/hashCode() methods.
   Those methods *cannot* be stubbed/verified.
   Mocking methods declared on non-public parent classes is not supported.
2. inside when() you don't call method on mock but on some other object.

    at com.yyy.CustomerAssignmentServiceTest.myTest(MyTest.java:576)

Here is Sample Test case

when(pageable.getSort()).thenReturn(Sort.by(order));
when(Sort.by(order).isSorted()).thenReturn(true);

when(pageable.getPageNumber()).thenReturn(0);
when(pageable.getPageSize()).thenReturn(1);
when(PageUtil.defaultSort(any(Pageable.class), "CONSTANT")).thenReturn(pageable);

myTestService.getAssignmentsForKnownContact("100", any(UUID.class), any(TestParams.class), pageable);

Here is another error:

org.mockito.exceptions.misusing.WrongTypeOfReturnValue: 
PageRequest cannot be returned by getSort()
getSort() should return Sort
***
If you're unsure why you're getting above error read on.
Due to the nature of the syntax above problem might occur because:
1. This exception *might* occur in wrongly written multi-threaded tests.
   Please refer to Mockito FAQ on limitations of concurrency testing.
2. A spy is stubbed using when(spy.foo()).then() syntax. It is safer to stub spies - 
   - with doReturn|Throw() family of methods. More in javadocs for Mockito.spy() method..
PAA
  • 1
  • 46
  • 174
  • 282

1 Answers1

0

As you said you are not mocking Sort.by the returned value is not a mock and the line

when(Sort.by(order).isSorted()).thenReturn(true);

will fail.

You could mock Sort.by using PowerMockito but I would recommend to not mock Sort.by but instead return a mock as the returned object of pageable.getSort()

Sort sortMock = Mockito.mock(Sort.class);
when(pageable.getSort()).thenReturn(sortMock);
when(sortMock.isSorted()).thenReturn(true);
brass monkey
  • 5,841
  • 10
  • 36
  • 61
  • Thanks, but I'm unable to mock `pageable = PageUtil.defaultSort(pageable, AppConst.SOMECONSTANT);` – PAA Nov 13 '19 at 16:19
  • When I do `when(PageUtil.defaultSort(pageable, "contactTypeCode")).thenReturn(pageable);`, I get the `org.mockito.exceptions.misusing.WrongTypeOfReturnValue: ` – PAA Nov 13 '19 at 16:21