【问题标题】:Mockito: Do extra actions before thenCallRealMethod()Mockito:在 thenCallRealMethod() 之前做额外的动作
【发布时间】:2021-08-12 04:20:24
【问题描述】:

在使用 Mockito 调用 thenCallRealMethod() 之前,我如何执行额外操作(例如记录、调用其他方法……)?

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

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

【问题讨论】:

    标签: java unit-testing junit mocking mockito


    【解决方案1】:

    您可以使用thenAnswer

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

    【讨论】:

      猜你喜欢
      • 2017-02-14
      • 1970-01-01
      • 1970-01-01
      • 2013-09-21
      • 1970-01-01
      • 2014-02-04
      • 1970-01-01
      • 1970-01-01
      • 2015-04-08
      相关资源
      最近更新 更多