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

查看:221
本文介绍了形成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:


当/然后:当(yourMethod())。thenReturn (5);

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

给定/意志:给定(yourMethod())。willThrow(OutOfMemoryException.class);

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

Do / When: 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();

验证/执行:验证(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 +)

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天全站免登陆