随笔 - 239  文章 - 0  评论 - 29  阅读 - 69万

mockito5.4.0单元测试(5) --校验mock对象的某种方法的准确调用次数 times

 

mokito官方文档地址: https://www.javadoc.io/doc/org.mockito/mockito-core/latest/org/mockito/Mockito.html#exact_verification

// mock一个对象
LinkedList mockedList = mock(LinkedList.class);
// 使用mock对象来操作
mockedList.add("once");   // 添加"once"一次

mockedList.add("twice");  // 添加"twice"一次
mockedList.add("twice");  // 添加"twice"二次

mockedList.add("three times");  // 添加"three times"一次
mockedList.add("three times");  // 添加"three times"二次
mockedList.add("three times");  // 添加"three times"三次

//following two verifications work exactly the same - times(1) is used by default
verify(mockedList).add("once");   // 校验是否添加了"once"
verify(mockedList, times(1)).add("once");  // 校验添加了"once"一次

//exact number of invocations verification
verify(mockedList, times(2)).add("twice");  // 校验添加了"twice"二次
verify(mockedList, times(3)).add("three times");  // 校验添加了"three times"三次

//verification using never(). never() is an alias to times(0)
verify(mockedList, never()).add("never happened");  // 校验从未添加过"never happened"

//verification using atLeast()/atMost()
verify(mockedList, atMostOnce()).add("once");  // 校验最多添加过一次"once"
verify(mockedList, atLeastOnce()).add("three times");  // 校验至少添加过一次"three times"
verify(mockedList, atLeast(2)).add("three times");  // 校验至少添加过二次"three times"
verify(mockedList, atMost(5)).add("three times");  // 校验最多添加过五次"three times"

 

 

end.

posted on   梦幻朵颜  阅读(319)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
历史上的今天:
2021-06-20 mysql5.7字段设计需要为非null,尤其是索引列,原理讲述

点击右上角即可分享
微信分享提示