单元测试中的计数方法调用
问题描述:
在单元测试中计算方法调用的最佳方法是什么.任何测试框架都允许吗?
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.
使用模拟,如果您正在测试列表,并且想验证是否使用这些参数调用了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 parameter you do the following:
List mock = mock(List.class);
someCodeThatInteractsWithMock();
verify(mock, times(3)).clear();
verify(mock, atLeastOnce()).add(anyObject());
(来自 http://code.google.com/p/mockito/wiki/MockitoVSEasyMock )