查找Mockito构造的导入静态语句 [英] Finding import static statements for Mockito constructs

查看:113
本文介绍了查找Mockito构造的导入静态语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图穿过我和Mockito之间的砖墙。为了获得Mockito东西的正确导入静态语句,我已经把头发撕掉了。你认为有人会抛出一张表,说 anyInt()来自 org.mockito.Matchers 当()来自 org.mockito.Mockito 等,但这对新手来说太有帮助了,不是吗?

I'm trying to crash through the brick wall between me and Mockito. I've torn my hair out over trying to get correct import static statements for Mockito stuff. You'd think someone would just throw up a table saying that anyInt() comes from org.mockito.Matchers and when() comes from org.mockito.Mockito, etc., but that would be too helpful to newcomers, no?

这类事情,尤其是混合时在无数的以星号结尾的import语句中,并不总是非常有用:

This sort of thing, especially when mixed in with myriad more import statements ending in asterisks, isn't always very helpful:

import static org.junit.Assert.*;
import static org.mockito.Mockito.*;

是的,我知道并且一直在尝试使用 Eclipse Window - > Preferences- > Java - > Editor-> Content Assist - > Favorites 机制。它有所帮助,但它没有击中头部。

Yes, I know about and have been trying to use the Eclipse Window -> Preferences-> Java -> Editor-> Content Assist -> Favorites mechanism. It helps, but it doesn't hit the nail on the head.

这个问题的任何答案都将不胜感激。

Any answers to this question would be appreciated.

非常感谢,
Russ

Many thanks, Russ

推荐答案

问题是来自 Hamcrest 和Mockito有相似的名称,但分别返回Matchers和实际值。

The problem is that static imports from Hamcrest and Mockito have similar names, but return Matchers and real values, respectively.

一种解决方法是简单地复制Hamcrest和/或Mockito类并删除/重命名静态函数,以便它们更容易记忆,并且更少显示在自动完成中。这就是我做的。

One work-around is to simply copy the Hamcrest and/or Mockito classes and delete/rename the static functions so they are easier to remember and less show up in the auto complete. That's what I did.

另外,当使用模拟时,我试图避免断言支持其他 c $ c>断言验证,例如

Also, when using mocks, I try to avoid assertThat in favor other other assertions and verify, e.g.

assertEquals(1, 1);
verify(someMock).someMethod(eq(1));

而不是

assertThat(1, equalTo(1));
verify(someMock).someMethod(eq(1));

如果从Eclipse中的收藏夹中删除类,并输入长名称,例如 org.hamcrest.Matchers.equalTo 并按CTRL + SHIFT + M进行'添加导入',然后自动完成只会显示Hamcrest匹配器,而不是任何Mockito匹配器。只要你不混合匹配器,你就可以用另一种方式做到这一点。

If you remove the classes from your Favorites in Eclipse, and type out the long name e.g. org.hamcrest.Matchers.equalTo and do CTRL+SHIFT+M to 'Add Import' then autocomplete will only show you Hamcrest matchers, not any Mockito matchers. And you can do this the other way so long as you don't mix matchers.

这篇关于查找Mockito构造的导入静态语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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