考虑这个示例类:
public class Processor {
private final Dependency dependency;
public Processor(Dependency dependency) {
this.dependency = dependency;
}
public void processUsers(List<Integer> userIds, int attempt) {
if (attempt == 0) {
//log initial processing
} else if (attempt > 0 && attempt < 5) {
//log retry processing
} else {
//log processing limit exceeded
return;
}
List<Integer> failedIds = new ArrayList<Integer>();
for (Integer userId : userIds) {
try {
processUser(userId);
} catch (Exception ex) {
//logging
failedIds.add(userId);
}
}
if (failedIds.isEmpty()) {
//log ok
} else {
processUsers(failedIds, attempt + 1);
}
}
public void processUser(Integer userId) throws Exception{
//dependency can throw exception
dependency.call();
}
}
我想验证方法 processUsers
在抛出异常时调用自身。
这是我的暴躁测试:
public class ProcessorTest {
@Test
public void processShouldCallItselfWithFailedSublistWhenProcessingFails(){
Dependency dependency = mock(Dependency.class);
when(dependency.call()).thenThrow(Exception.class);
Processor processor = new Processor(dependency);
processor.processUsers(Arrays.asList(new Integer[]{1,2,3}), 0);
//need to verify processor will call #processUsers recursively
//because the dependency thrown Exception, how?
}
}
验证该方法在特定情况下递归调用自身的最佳做法是什么?
我正在使用 TestNG + Mockito 和这种称为 JAVA
的冗长语言
最佳答案
您可以验证 dependency.call()
被调用的次数。这会告诉您重试有效 - 仅通过调用次数:
@Test
public void processShouldCallItselfWithFailedSublistWhenProcessingFails() {
Dependency dependency = mock(Dependency.class);
when(dependency.call()).thenReturn(1, 2).thenThrow(Exception.class).
thenReturn(4);
Processor processor = new Processor(dependency);
processor.processUsers(Arrays.asList(new Integer[]{1, 2, 3}), 0);
verify(dependency, times(4)).call();
}
这会告诉您重试最终因异常太多而失败:
@Test
public void processShouldFailAfterTooManyRetries() {
Dependency dependency = mock(Dependency.class);
when(dependency.call()).thenThrow(Exception.class);
Processor processor = new Processor(dependency);
final List<Integer> userIds = Arrays.asList(new Integer[]{1, 2, 3});
final int expectedRetries = 5;
processor.processUsers(userIds, 0);
verify(dependency, times(expectedRetries * userIds.size())).call();
}
如果对依赖项的调用实际上使用了 userId,那么您实际上可以检查是否所有预期的对 dependency.call(int userId)
的调用都发生了。这会告诉你,如果有足够的重试,所有这些都通过了:
@Test
public void processShouldCallItselfWithFailedSublistWhenProcessingFails() {
Dependency dependency = mock(Dependency.class);
when(dependency.call(anyInt())).
thenReturn(1, 2).thenThrow(Exception.class).thenReturn(4);
Processor processor = new Processor(dependency);
processor.processUsers(Arrays.asList(new Integer[]{1, 2, 3}), 0);
verify(dependency).call(1);
verify(dependency).call(2);
verify(dependency, times(2)).call(3);
}
@Test
public void processShouldFailAfterTooManyRetries() {
Dependency dependency = mock(Dependency.class);
when(dependency.call(anyInt())).thenThrow(Exception.class);
Processor processor = new Processor(dependency);
final List<Integer> userIds = Arrays.asList(new Integer[]{1, 2, 3});
final int expectedRetries = 5;
processor.processUsers(userIds, 0);
for (Integer userId : userIds) {
verify(dependency, times(expectedRetries)).call(userId);
}
}
关于java - 使用 Mockito 验证递归方法调用的最佳实践,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15835266/