org.mockito.exceptions.misusing.UnfinishedStubbingException随机发生

时间:2019-09-10 13:16:12

标签: java junit mockito jboss-arquillian

我在Arquillian测试中遇到了此异常,该测试使用Mockito(版本1.9.4相当老)来模拟其他服务。

这是我的代码

@Before
    public void mockPairingService() {
        pairingService = mockService(pairingService, PairingService.class);

        when(pairingService.createPairingByAcquirer(any(PairingData.class))).thenAnswer(
                new Answer<PairingInformation>() {
                    @Override
                    public PairingInformation answer(InvocationOnMock invocation) {
                        PairingData invoPairingData = (PairingData) invocation.getArguments()[0];
                        PairingInformation pairingInformation = new PairingInformation();

                        if (invoPairingData != null
                                && invoPairingData.getTerminalPairingData() != null) {
                            TerminalPairingData invoTerminalPairingData = invoPairingData.getTerminalPairingData();
                            orderToAddToMockPairingInformation = invoTerminalPairingData.getOrderUuid();
                            // Terminal 1 - pairing INITIATED in createToken and PAIRED in findPairing
                            if (TERMINAL_EXTID.equals(invoTerminalPairingData.getTerminalExtId())) {
                                pairingInformation = createPairingInfoForMock(MERCHANT_UUID, TERMINAL_UUID,
                                        orderToAddToMockPairingInformation, PAIRING_UUID, PairingStateEnum.INITIATED,
                                        null, null, null);
                            }
                            // Terminal 2 - PAIRED
                            else if (TERMINAL_EXTID2.equals(invoTerminalPairingData.getTerminalExtId())) {
                                pairingInformation = createPairingInfoForMock(MERCHANT_UUID, TERMINAL_UUID,
                                        orderToAddToMockPairingInformation, PAIRING_UUID, PairingStateEnum.PAIRED,
                                        PRIVATECUSTOMER_UUID, null, null);
                            }
                        }
                        return pairingInformation;
                    }
                });

        PairingInformation pairingInformation = createPairingInfoForMock(MERCHANT_UUID, TERMINAL_UUID,
                orderToAddToMockPairingInformation, PAIRING_UUID, PairingStateEnum.PAIRED, PRIVATECUSTOMER_UUID,
                new CouponList(), CouponRedemptionTypeEnum.FULL);

        doReturn(pairingInformation).when(pairingService).findNewestPairingInformation(eq(TERMINAL_UUID), any());
        doReturn(pairingInformation).when(pairingService).findActivePairingInformation(any(UUID.class), eq(TERMINAL_UUID), any());

        pairingInformation = createPairingInfoForMock(MERCHANT_UUID, TERMINAL_UUID2, orderToAddToMockPairingInformation,
                PAIRING_UUID, PairingStateEnum.PAIRED, PRIVATECUSTOMER_UUID, new CouponList(), CouponRedemptionTypeEnum.FULL);

doReturn(pairingInformation).when(pairingService).findNewestPairingInformation(eq(TERMINAL_UUID2), any());
            doReturn(pairingInformation).when(pairingService).findActivePairingInformation(any(UUID.class), eq(TERMINAL_UUID2), any());

        mockPairingService_findPairingByUuid(PairingStateEnum.PAIRED);
    }

还有例外:

org.mockito.exceptions.misusing.UnfinishedStubbingException: 
Unfinished stubbing detected here:
-> at ch.adnovum.monexio.server.orderserviceimpl.PaymentOrchestrationBeanTest.mockPairingService(PaymentOrchestrationBeanTest.java:664)

E.g. thenReturn() may be missing.
Examples of correct stubbing:
    when(mock.isOk()).thenReturn(true);
    when(mock.isOk()).thenThrow(exception);
    doThrow(exception).when(mock).someVoidMethod();
Hints:
 1. missing thenReturn()
 2. you are trying to stub a final method, you naughty developer!

    at ch.adnovum.monexio.server.orderserviceimpl.PaymentOrchestrationBeanTest.mockPairingService(PaymentOrchestrationBeanTest.java:664)
    at sun.reflect.GeneratedMethodAccessor448.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    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.RunBefores.evaluate(RunBefores.java:24)
    at org.jboss.arquillian.junit.Arquillian$StatementLifecycleExecutor.invoke(Arquillian.java:404)
    at org.jboss.arquillian.container.test.impl.execution.BeforeLifecycleEventExecuter.on(BeforeLifecycleEventExecuter.java:34)
    at sun.reflect.GeneratedMethodAccessor423.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.jboss.arquillian.core.impl.ObserverImpl.invoke(ObserverImpl.java:86)
    at org.jboss.arquillian.core.impl.EventContextImpl.invokeObservers(EventContextImpl.java:103)
    at org.jboss.arquillian.core.impl.EventContextImpl.proceed(EventContextImpl.java:90)
    at org.jboss.arquillian.test.impl.TestContextHandler.createTestContext(TestContextHandler.java:116)
    at sun.reflect.GeneratedMethodAccessor406.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.jboss.arquillian.core.impl.ObserverImpl.invoke(ObserverImpl.java:86)
    at org.jboss.arquillian.core.impl.EventContextImpl.proceed(EventContextImpl.java:95)
    at org.jboss.arquillian.test.impl.TestContextHandler.createClassContext(TestContextHandler.java:83)
    at sun.reflect.GeneratedMethodAccessor405.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)

这种异常有时会在运行整个测试期间发生。如果我运行1个特定的测试,则不会发生。

我在这一行有例外。

doReturn(pairingInformation).when(pairingService).findNewestPairingInformation(eq(TERMINAL_UUID2), any());

所以不确定如何解决它。

任何解决方案将不胜感激。谢谢。

0 个答案:

没有答案