EasyMock:AssertionError - 意外的方法调用:具有多个模拟对象的多个expecations

时间:2016-05-25 22:50:08

标签: easymock

我是EasyMock的新手并在我的项目中实现了JUnits。 下面是我的Test类,通过查看代码,你可以理解我在这里做什么。我正在使用EasyMock 3.4。

简而言之: 我正在为我的实际方法FmrTradeConsumerImpl.onTODEvent(TodEvents todEvents)编写一个Junit。

myTestClass:

@RunWith(EasyMockRunner.class)
public class FmrTradeConsumerImplTest extends EasyMockSupport {

    @TestSubject
    private FmrTradeConsumerImpl fmrTradeConsumer = new FmrTradeConsumerImpl();
    @Mock
    private TradeService tradeService;
    @Mock
    private TradeIvTypeValidator tradeValid;
    @Mock
    private TodEvents todEvents;

    private List<TradeDetails> tradesList;
    private SecurityTradeExecution tradeEx;
    private List<SecurityTradeExecution> securityTradeEx;

    @Before
    public void init() {

        tradeService = niceMock(TradeService.class);
        todEvents = niceMock(TodEvents.class);
        securityTradeEx = new ArrayList<>();
        tradesList = new ArrayList<>();
        tradeEx = new SecurityTradeExecution();
        tradeValid = niceMock(TradeIvTypeValidator.class);
        resetAll();
    }

    @Test
    public void onTODEvent() {
        tradeEx.setFundNumber(Long.valueOf("2016"));
        tradeEx.setSourceSystemCd("AIM");
        tradeEx.setIvType("Test");
        securityTradeEx.add(tradeEx);

         expect(todEvents.getSecurityTradeExcecutions()).andReturn(securityTradeEx);
        expect(tradeValid.isValidIvType("Test")).andReturn(true);

        tradeService.saveTradeRecordsListAsBatch(tradesList);
        expectLastCall().andVoid();

        replayAll();

        fmrTradeConsumer.onTODEvent(todEvents);
    }
}

我收到了以下错误:

java.lang.AssertionError: 
  Unexpected method call TradeIvTypeValidator.isValidIvType("Test"):
    at org.easymock.internal.MockInvocationHandler.invoke(MockInvocationHandler.java:44)
    at org.easymock.internal.ObjectMethodsFilter.invoke(ObjectMethodsFilter.java:94)
    at org.easymock.internal.ClassProxyFactory$MockMethodInterceptor.intercept(ClassProxyFactory.java:97)
    at com.fmr.ftg.es.mas.tod.client.TradeIvTypeValidator$$EnhancerByCGLIB$$311c4357.isValidIvType(<generated>)
    at com.fmr.ftg.es.mas.tod.client.impl.FmrTradeConsumerImpl.isValidTrade(FmrTradeConsumerImpl.java:120)
    at com.fmr.ftg.es.mas.tod.client.impl.FmrTradeConsumerImpl.onTODEvent(FmrTradeConsumerImpl.java:67)
    at com.fmr.ftg.es.mas.tod.client.impl.FmrTradeConsumerImplTest.onTODEvent(FmrTradeConsumerImplTest.java:78)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)

请提供您的建议, 非常感谢你。

2 个答案:

答案 0 :(得分:0)

不使用“私有TradeIvTypeValidator tradeValid;”的“@Mock”注释,而是使用“@NiceMock”。 这里NiceMock提供默认实现,如果你使用“@Mock”,那么它可以让开发人员做同样的事情

答案 1 :(得分:0)

为什么要在创建后重置所有模拟?你可以删除该行并尝试它是否有效