计算单元测试中的方法调用 [英] Counting method invocations in Unit tests

查看:15
本文介绍了计算单元测试中的方法调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在单元测试中计算方法调用的最佳方法是什么.是否有任何测试框架允许这样做?

What is the best way to count method invocations in a Unit Test. Do any of the testing frameworks allow that?

推荐答案

听起来你可能想使用模拟框架通常提供的 .expects(1) 类型的方法.

It sounds like you may want to be using the .expects(1) type methods that mock frameworks usually provide.

使用 mockito,如果您正在测试一个 List 并且想要验证 clear 被调用了 3 次并且 add 被调用了至少一次,那么您可以执行以下操作:

Using mockito, if you were testing a List and wanted to verify that clear was called 3 times and add was called at least once with these parameters you do the following:

List mock = mock(List.class);        
someCodeThatInteractsWithMock();                 

verify(mock, times(3)).clear();
verify(mock, atLeastOnce()).add(anyObject());      

来源 - MockitoVsEasyMock

这篇关于计算单元测试中的方法调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆