在我的情况下,Mockito 验证一个函数被调用一次

Mockito verify a function is invoked once in my case(在我的情况下,Mockito 验证一个函数被调用一次)
本文介绍了在我的情况下,Mockito 验证一个函数被调用一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我正在使用 Mockito 来编写我的测试用例.我有一个简单的类,其中包含一个我有兴趣测试的函数 countPerson(boolean):

I am using Mockito to write my test case. I have a simple class which contains a function countPerson(boolean) which I am interested to test:

public class School {
  //School is a singleton class.

  public void countPerson(boolean includeTeacher) {
       if (includeTeacher) {
          countIncludeTeacher();
          return;
       }
       countOnlyStudents();
  }

  public void countIncludeTeacher() {...}
  public void countOnlyStudents() {...}
}

在我的单元测试中,我想测试 countPerson(boolean) 函数:

In my unit test, I want to test the countPerson(boolean) function:

public class SchoolTest{
   private School mSchool;
   @Before
   public void setUp(){
      mSchool = School.getInstance();
   }
   @Test 
   public void testCountPerson() {
       mSchool.countPerson(true);
       //How to test/verify countIncludeTeacher() is invoked once?
   }
}

如何使用 Mockito 检查/验证 countIncludeTeacher() 在我的测试用例中被调用一次?

How to use Mockito to check/verify countIncludeTeacher() is invoked once in my test case?

推荐答案

你必须使用 间谍.这里的问题是您想验证一个方法是在 real 对象上调用的,而不是在模拟对象上调用的.你不能在这里使用模拟,因为它会存根类中的所有方法,因此也存根 countPerson 默认不做任何事情.

You will have to use a spy. The problem here is that you want to verify that a method was called on a real object, not on a mock. You can't use a mock here, since it will stub all the methods in the class, thereby stubbing also countPerson to do nothing by default.

@Test 
public void testCountPerson() {
    School school = School.getInstance();
    School spySchool = Mockito.spy(school);
    spySchool.countPerson(true);
    verify(spySchool).countIncludeTeacher();
}

但是,请注意,在使用间谍时应该非常小心,因为除非被存根,否则真正的方法是被调用的.引用 Mockito Javadoc:

However, note that you should be very careful when using spies because, unless stubbed, the real methods are gettings called. Quoting Mockito Javadoc:

应该小心偶尔使用真正的间谍,例如在处理遗留代码时.

Real spies should be used carefully and occasionally, for example when dealing with legacy code.

这篇关于在我的情况下,Mockito 验证一个函数被调用一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Sending a keyboard event from java to any application (on-screen-keyboard)(将键盘事件从 java 发送到任何应用程序(屏幕键盘))
How to make JComboBox selected item not changed when scrolling through its popuplist using keyboard(使用键盘滚动其弹出列表时如何使 JComboBox 所选项目不更改)
Capturing keystrokes without focus(在没有焦点的情况下捕获击键)
How can I position a layout right above the android on-screen keyboard?(如何将布局放置在 android 屏幕键盘的正上方?)
How to check for key being held down on startup in Java(如何检查在Java中启动时按住的键)
Android - Get keyboard key press(Android - 获取键盘按键)