Java EasyMock - 匹配器与原始值混合?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22512927/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 16:11:17  来源:igfitidea点击:

EasyMock - matchers are mixed with raw values?

javaeasymock

提问by emeraldjava

I have this method signature that i want to mock with EasyMock

我有这个方法签名,我想用 EasyMock 来模拟

public BigDecimal getRemainingPremium(BigDecimal baseAmount, Date commencementDate, Date effectiveDate, boolean isComplete)

My test code has

我的测试代码有

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();

but i keep getting this matcher exception. I've tried all combinations of primitives and 'EasyMock.anyObject(Boolean.class)'. Any suggestions on a workaround?

但我不断收到此匹配器异常。我已经尝试了原语和“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)

采纳答案by Dan Temple

You're using a matcher where you should to use an actual object.

您正在使用匹配器,您应该在其中使用实际对象。

EasyMock.expect(premium.getCommencementDate()).andReturn(EasyMock.anyObject(Date.class)).anyTimes();

In the line above, you have used the anyObject()matcher where you really mean to use a Dateobject.

在上面的行中,您anyObject()在真正要使用Date对象的地方使用了匹配器。

I wonder if you are confusing matchers with mocks in this sense. The anyObject()matcher is a way of confirming that the method you have mocked is called with an object of type Date. It does not create a date object that can be used as an instance of the Dateclass. For that you would need to create a mock instance of Date. So, keep in mind that matchers should be used as parameters to mocked methods, but not as return values.

我想知道您是否在这个意义上将匹配器与模拟混淆了。anyObject()匹配器是一种确认您模拟的方法是使用类型为 的对象调用的方法Date。它不会创建可用作Date类实例的日期对象。为此,您需要创建Date. 因此,请记住,匹配器应该用作模拟方法的参数,而不是用作返回值。

The below expectations will fix your issue:

以下期望将解决您的问题:

    Date mockDate = EasyMock.createMock(Date.class);
    final IPremium premium = EasyMock.createMock(IPremium.class);
    EasyMock.expect(premium.getCommencementDate()).andReturn(mockDate).anyTimes();
    expect(
            premium.getRemainingPremium(
                    (BigDecimal) EasyMock.anyObject(),
                    (Date) EasyMock.anyObject(),
                    (Date) EasyMock.anyObject(),
                    EasyMock.anyBoolean()
                    ))
                    .andReturn(BigDecimal.TEN).anyTimes();
    replay(premium);