Mockito:在 thenCallRealMethod() 之前执行额外的操作

Mockito: Do extra actions before thenCallRealMethod()

如何在使用 Mockito 调用 thenCallRealMethod() 之前执行额外的操作(例如记录日志、调用其他方法...)?

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

有没有类似上面代码中的pipe()方法的东西?

你可以用 thenAnswer:

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