Search code examples
javaunit-testingjunitmockingmockito

Mockito: Do extra actions before thenCallRealMethod()


How can I do extra actions (e.g. logging, call other methods,...) before calling thenCallRealMethod() using Mockito ?

Mockito.when(student.calculateGrade())
       .pipe(() -> {
           System.out.println("blabla");
           doSomethingElse("...");
       }).thenCallRealMethod();

Is there anything like the pipe() method in the code above ?


Solution

  • You can do it with thenAnswer:

    when(student.calculateGrade()).thenAnswer(invocation -> {
                System.out.println("Log something");
                return invocation.callRealMethod();
            });