问题描述
我正试图冲破我和 Mockito 之间的砖墙.试图为 Mockito 的东西获取正确的 import static 语句时,我已经把头发扯掉了.你会认为有人会扔一张桌子说 anyInt() 来自 org.mockito.Matchers 而 when() 来自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?
这种事情,尤其是与无数以星号结尾的导入语句混合时,并不总是很有帮助:
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.
非常感谢,拉斯
推荐答案
问题是静态导入从 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.
另外,在使用 mocks 时,我尽量避免使用 assertThat
以支持其他其他 assertions
和 verify
,例如
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 构造的导入静态语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!