EasyMock - Combine Combinations with Raw Values?

I have this method signature that I want to laugh with EasyMock

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

In my test code there is

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 exception. I tried all combinations of primitives and "EasyMock.anyObject (Boolean.class)". Any suggestions on a workaround?

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)
+4
source share
1 answer

You use a helper where you must use the actual object.

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

In the line above, you used a match anyObject()where you really want to use the object Date.

, . anyObject() - , , , Date. , Date. Date. , , .

:

    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);
+8

Source: https://habr.com/ru/post/1532518/


All Articles