EasyMock : java.lang.IllegalStateException: 1 个匹配器,2 个记录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6550036/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
EasyMock : java.lang.IllegalStateException: 1 matchers expected, 2 recorded
提问by Anne
I am having a problem with EasyMock 2.5.2 and JUnit 4.8.2 (running through Eclipse). I have read all the similar posts here but have not found an answer. I have a class containing two tests which test the same method. I am using matchers.
我在使用 EasyMock 2.5.2 和 JUnit 4.8.2(通过 Eclipse 运行)时遇到问题。我在这里阅读了所有类似的帖子,但没有找到答案。我有一个包含两个测试相同方法的测试的类。我正在使用匹配器。
- Each test passes when run alone.
- The first test always passes - this is true if I switch the order of the tests in the file.
- 每个测试在单独运行时都通过。
- 第一个测试总是通过 - 如果我切换文件中测试的顺序,这是真的。
Here is a simplified version of the test code:
这是测试代码的简化版本:
private Xthing mockXthing;
private MainThing mainThing;
@Before
public void setUp() {
mockXthing = EasyMock.createMock(Xthing.class);
mainThing = new MainThing();
mainThing.setxThing(mockXthing);
}
@After
public void cleanUp() {
EasyMock.reset(mockXthing);
}
@Test
public void testTwo() {
String abc = "abc";
EasyMock.expect(mockXthing.doXthing((String) EasyMock.anyObject())).andReturn(abc);
EasyMock.replay(mockXthing);
String testResult = mainThing.testCallingXthing((Long) EasyMock.anyObject());
assertEquals("abc", testResult);
EasyMock.verify(mockXthing);
}
@Test
public void testOne() {
String xyz = "xyz";
EasyMock.expect(mockXthing.doXthing((String) EasyMock.anyObject())).andReturn(xyz);
EasyMock.replay(mockXthing);
String testResult = mainThing.testCallingXthing((Long) EasyMock.anyObject());
assertEquals("xyz", testResult);
EasyMock.verify(mockXthing);
}
The second (or last) test always fails with the following error:
第二次(或最后一次)测试总是失败并出现以下错误:
java.lang.IllegalStateException: 1 matchers expected, 2 recorded
Any insight to this would be greatly appreciated.
对此的任何见解将不胜感激。
Thanks, Anne
谢谢,安妮
采纳答案by Mark Peters
I haven't looked meticulously closely yet, but this looks suspect:
我还没有仔细观察过,但这看起来很可疑:
String testResult = mainThing.testCallingXthing((Long) EasyMock.anyObject());
anyObject()
is a matcher and you're calling it afterthe replay. It's not used to produce any object. It's used to instruct EasyMock to allowany object. EasyMock is detecting that extra matcher but it is not harmful until the second test. At that point, the number of matchers that EasyMock has recorded but hasn't yet used (2) doesn't line up with the number of parameters expected for the second doXthing
call (1).
anyObject()
是一个匹配器,你在重播后调用它。它不用于生成任何对象。它用于指示 EasyMock允许任何对象。EasyMock 正在检测那个额外的匹配器,但在第二次测试之前它没有害处。此时,EasyMock 已记录但尚未使用的匹配器数量 (2) 与第二次doXthing
调用所需的参数数量 (1) 不一致。
You should be passing in realparameters to testCallingXthing
(or a mock that is in replay mode). Try passing in null
directly, or a real value like 2
.
您应该将实际参数传递给testCallingXthing
(或处于重播模式的模拟)。尝试null
直接传入,或像2
.
回答by nsfyn55
Try:
尝试:
String testResult = mainThing.testCallingXthing(eq(EasyMock.anyLong()));
There are more refined matchers than anyObject()
. These allow you to make type-based assertions about collaborators.
有比 更精致的匹配器anyObject()
。这些允许您对协作者进行基于类型的断言。
From the EasyMock documentation:
eq(X value)
Matches if the actual value is equals the expected value. Available for all primitive types and for objects.anyBoolean()
,anyByte()
,anyChar()
,anyDouble()
,anyFloat()
,anyInt()
,anyLong()
,anyObject()
,anyShort()
eq(X value)
如果实际值等于预期值,则匹配。适用于所有原始类型和对象。anyBoolean()
,anyByte()
,anyChar()
,anyDouble()
,anyFloat()
,anyInt()
,anyLong()
,anyObject()
,anyShort()
回答by rogerdpack
for me this failure (in my case 2 matchers expected, 4 recorded.) meant "you are mixing easymock and mockito in the same unit test, so accidentally calling easymock's notNull() method for a mockito argument. Which causes the failure but only if the tests are run in a certain order.
对我来说,这个失败(在我的例子中预计有 2 个匹配器,4 个记录。)意味着“你在同一个单元测试中混合了 easymock 和 mockito,所以不小心调用了 easymock 的 notNull() 方法来获取 mockito 参数。这会导致失败,但前提是测试按特定顺序运行。
回答by user1371983
You should reset mock after each test method to get rid of this problem. Adding below code will solve this problem.
您应该在每个测试方法之后重置模拟以摆脱这个问题。添加以下代码将解决此问题。
@After
public void after(){
EasyMock.reset(mockXthing)
}