【发布时间】:2014-03-19 16:56:58
【问题描述】:
我有这个方法签名,我想用 EasyMock 模拟
public BigDecimal getRemainingPremium(BigDecimal baseAmount, Date commencementDate, Date effectiveDate, boolean isComplete)
我的测试代码有
Premium premium = createMock(Premium.class);
// add this line
EasyMock.expect(premium.getCommencementDate()).andReturn(EasyMock.anyObject(Date.class)).anyTimes();
expect(
premium.getRemainingPremium(
EasyMock.anyObject(BigDecimal.class),
EasyMock.anyObject(Date.class),
EasyMock.anyObject(Date.class),
EasyMock.anyBoolean()
))
.andReturn(BigDecimal.TEN).anyTimes();
但我不断收到此匹配器异常。我已经尝试了原语和“EasyMock.anyObject(Boolean.class)”的所有组合。关于解决方法的任何建议?
java.lang.IllegalStateException: 4 matchers expected, 5 recorded.
This exception usually occurs when matchers are mixed with raw values when recording a method:
foo(5, eq(6)); // wrong
You need to use no matcher at all or a matcher for every single param:
foo(eq(5), eq(6)); // right
foo(5, 6); // also right
at org.easymock.internal.ExpectedInvocation.createMissingMatchers(ExpectedInvocation.java:48)
at org.easymock.internal.ExpectedInvocation.<init>(ExpectedInvocation.java:41)
at org.easymock.internal.RecordState.invoke(RecordState.java:79)
at org.easymock.internal.MockInvocationHandler.invoke(MockInvocationHandler.java:41)
【问题讨论】:
-
我会添加你的整个测试。通常当你得到
foo(5, eq(6)); // wrong错误时,是因为你没有使用足够的匹配器,但是这里使用的匹配器的数量比预期的要多,所以我怀疑你在调用 replay 后使用了匹配器。跨度> -
@DanTemple 抱歉 - 我用额外的行更新了测试代码(我认为它不会影响测试)。这解释了第 5 个匹配器的出现位置,但我不确定调用 replay() 将如何影响这一点?