形成 Mockito “语法" [英] Forming Mockito "grammars"

查看:34
本文介绍了形成 Mockito “语法"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Mockito 似乎是一个非常棒的 Java 存根/模拟框架.唯一的问题是我找不到有关使用其 API 的最佳方法的任何具体文档.测试中常用的方法包括:

Mockito seems like a pretty sweet stubbing/mocking framework for Java. The only problem is I can't find any concrete documentation on the best ways of using their API. Common methods used in tests include:

doXXX(???) : Stubber
when(T) : OngoingStubbing
then(T) : OngoingStubbing
verify(???) : T
given(T) : BDDOngoingStubbing
willXXX(???) : BDDStubber

当您在实践中看到 Mockito 示例时,您会看到如下代码:

When you see examples of Mockito in practice, you see code like:

when(yourMethod()).thenReturn(5);

从我读过的所有文档中,我确定了几个模式":Mockito 的语法"通过菊花链将这些方法调用组合在一起获得,就像上面的例子一样.我发现的一些常见模式是:

From all the docs I've read, I've identified several "patterns" of Mockito "grammars" obtained from daisy-chaining these method calls together like the example above. Some common patterns I've found are:

何时/那么: when(yourMethod()).thenReturn(5);

When/Then: when(yourMethod()).thenReturn(5);

Given/Will: given(yourMethod()).willThrow(OutOfMemoryException.class);

Given/Will: given(yourMethod()).willThrow(OutOfMemoryException.class);

做/何时: doReturn(7).when(yourMock.fizzBu​​zz());

Do/When: doReturn(7).when(yourMock.fizzBuzz());

Will/Given/Do: willReturn(any()).given(yourMethod()).doNothing();

Will/Given/Do: willReturn(any()).given(yourMethod()).doNothing();

验证/执行: verify(yourMethod()).doThrow(SomeException.class);

Verify/Do: verify(yourMethod()).doThrow(SomeException.class);

让我窒息的是如何选择正确的方法调用模式/组合来为我的测试用例建模.似乎您可以将这些菊花链组合成看似无穷无尽的组合,但我不确定哪种模式适合哪个问题.

What I'm choking on is how to select the right pattern/combination of method calls to model my test cases. It seems like you can daisy-chain these together in seemingly endless combos and I'm not sure what pattern is right for which problem.

某些 Mockito Guru 能否帮助阐明 Mockito 方法的哪些模式/组合用于哪些类型的测试用例(以及为什么)?提前致谢!

Can some Mockito Guru help shed some light as to which patterns/combinations of Mockito methods are used for which types of test cases (and why)? Thanks in advance!

推荐答案

Mockito 通常有多种做事方式.

Mockito often have several ways of doing things.

我发现自己主要使用:

// Setup expectations
when(object.method()).thenReturn(value);
when(object.method()).thenThrow(exception);
doThrow(exception).when(object.voidMethod());


// verify things
verify(object, times(2)).method();
verify(object, times(1)).voidMethod();

我发现通过这三种调用,我可以完成 95% 的任务.

I've found that i can do 95% of what i need to with these three kinds of calls.

另外,您使用的是什么版本的 Mockito?最新版本 (1.9.0+) 中不存在given"和will"结构

Also, what version of Mockito are you using? "given" and "will" constructs are not present in the latest version (1.9.0+)

但是,在某些情况下,我希望返回值或异常响应输入.在这种情况下,您可以使用 Answer 接口来检查方法参数并返回适当的值.

However, there are cases where I want the return value or exception to respond to the input. In this case, you can use the Answer interface to inspect the method arguments and return an appropriate value.

public class ReturnFirstArg<T> implements Answer<T> {
    public T answer(InvocationOnMock invocation) {
        return invocation.getArguments()[0];
    }
}

when(object.method(7)).thenAnswer(new ReturnFirstArg<Integer>());

这篇关于形成 Mockito “语法"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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