Mockito验证一个方法在间谍之后被调用一次,尽管它永远不会被调用



我有一个单元测试,一开始就发现了一个奇怪的问题

    GSLClient gslClient = spy(new GSLSolaceClient());
    String orgId = "10006";
    int orgFlag = Destination.ORG_ID_INPUT_FLAG_APP;
    String eventId = "11010000";
    String scenario = "01";
    String inputDCN = "A00";
    String GSLRetDCN = "FT0";
    //mock
    when(gslClient.requestTargetDCNFromGSLServer(mockGSLBizSeqNo,orgId, eventId, scenario, orgFlag))
            .thenReturn(buildMockSuccessGSLRespMsg(GSLResponse.ORG_FALG_GSL_INPUT, GSLRetDCN))//success for first time
            .thenThrow(new GSLClient.GSLInvokeTimeoutException("Mock GSL timeout"));//timout for the second time
    verify(gslClient, never()).requestTargetDCNFromGSLServer(mockGSLBizSeqNo,orgId, eventId, scenario, orgFlag);

除了模拟我的对象之外,我什么也没做,然后我验证 mock 方法从未被调用。

但结果给了我:

org.mockito.exceptions.verification.NeverWantedButInvoked: 
gSLSolaceClient.requestTargetDCNFromGSLServer(
    "MOCKGSLBIZSEQNO_GSL_CACHE_TEST",
    "10006",
    "11010000",
    "01",
    0
);
Never wanted here:
-> at cn.webank.rmb.gsl.GSLCacheTest.test_76789232(GSLCacheTest.java:226)
But invoked here:
-> at cn.webank.rmb.gsl.GSLCacheTest.test_76789232(GSLCacheTest.java:222)

    at cn.webank.rmb.gsl.GSLCacheTest.test_76789232(GSLCacheTest.java:226)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    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:271)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:237)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

我想知道是否有其他地方调用此方法,所以我尝试删除代码

        when(gslClient.requestTargetDCNFromGSLServer(mockGSLBizSeqNo,orgId, eventId, scenario, orgFlag))
            .thenReturn(buildMockSuccessGSLRespMsg(GSLResponse.ORG_FALG_GSL_INPUT, GSLRetDCN))//success for first time
            .thenThrow(new GSLClient.GSLInvokeTimeoutException("Mock GSL timeout"));//timout for the second time

然后结果被传递,证明实际上没有其他地方在调用此方法。

但是为什么莫皮托会给我这个结果呢?

我已经尝试了以下测试,但它通过了!

   {
        //test mock
        Date mockD = spy(new Date());
        when(mockD.getTime())
                .thenReturn(1l)
                .thenThrow(new RuntimeException("mock ex"));
        verify(mockD, never()).getTime();
    }

-------------------方法的起源requestTargetDCNFromGSLServer就像--------------

Message requestTargetDCNFromGSLServer(String bizSeqNo, String targetOrgId,String serviceEventId,String scenario,int orgFlag) throws RMBValidationException, RMBIllegalAccessException, GSLInvokeTimeoutException {
    System.out.println("-------------------------------------------------------requestTargetDCNFromGSLServer");//I can only see this log one time
    SysHeader msgHeader = Util.createSysHeader(bizSeqNo,bizSeqNo, SOURCESYSID);
    Destination destination = Util.createSimpleDestination(GSL_SERVICEID,GSL_SCENARIO,GSL_DCN);
    Message reqMessage = Util.createMessage(msgHeader, GSL_APPHEADER,destination, createRequestContent(targetOrgId,serviceEventId,scenario,orgFlag));
    IMessagePublisher synPublisher = getSyncPublisherInstance();
    reqMessage.setTimeToLive(Global.onlyInstance().getGslRequestTimeout());
    Message rspMessage = synPublisher.publish(reqMessage, Global.onlyInstance().getGslRequestTimeout()) ;
    if(rspMessage == null) {
        throw new GSLClient.GSLInvokeTimeoutException("Cannot recieve GSL response at "+ Global.onlyInstance().getGslRequestTimeout()+" ms");
    }else {
        return rspMessage;
    }
}

你在嘲笑间谍,所以你应该坚持这个嘲笑顺序:

    doReturn(buildMockSuccessGSLRespMsg(GSLResponse.ORG_FALG_GSL_INPUT, GSLRetDCN))
    .doThrow(new GSLClient.GSLInvokeTimeoutException("Mock GSL timeout"))
    .when(gslClient).requestTargetDCNFromGSLServer(
             mockGSLBizSeqNo,orgId, eventId, scenario, orgFlag);

在你目前的情况下,你在模拟时调用一个真正的实现,这就是你得到错误的原因。当您开始配置时,预期的内容是Mockito可以"保护"真正的方法调用when方法中的间谍。

相关内容

  • 没有找到相关文章

最新更新