【问题标题】:I am trying to mock a final java class using Mockito 2, class is getting mocked but I still get Unfinished Stubbing Exception我正在尝试使用 Mockito 2 模拟最终的 java 类,类被模拟但我仍然得到未完成的存根异常
【发布时间】:2020-04-03 05:20:52
【问题描述】:

我也包含了 MockMaker 文件-src\test\resources\mockito-extensions\org.mockito.plugins.MockMaker

相关代码如图,其中 SignatureValidator 为 final 类-

mockValidator = mock(org.opensaml.xmlsec.signature.support.SignatureValidator.class);
mockSignature = mock(SignatureImpl.class);
mockCredential = mock(org.opensaml.security.credential.Credential.class);  

@Test(expected = SamlSecurityException.class)
    public void testGivenGoodProfileButInvalidSignature() throws SignatureException {
        when(mockSamlToken.getSignature()).thenReturn(mockSignature);
        when(mockSamlToken.getSAMLIssuerName()).thenReturn("fakeIssuerName");
        doThrow(SignatureException.class).when(mockValidator).validate(mockSignature,mockCredential); // getting exception for this line

        validator.validate(mockSamlToken);
    }

堆栈跟踪-

java.lang.Exception: Unexpected exception, expected<com.cerner.cto.security.saml.SamlSecurityException> but was<org.mockito.exceptions.misusing.UnfinishedStubbingException>
    at org.junit.internal.runners.statements.ExpectException.evaluate(ExpectException.java:28)
    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.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.runners.ParentRunner.run(ParentRunner.java:309)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:538)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)
Caused by: org.mockito.exceptions.misusing.UnfinishedStubbingException: 
Unfinished stubbing detected here:
-> at com.cerner.cto.security.saml.opensaml.SignatureValidatorTest.testGivenGoodProfileButInvalidSignature(SignatureValidatorTest.java:84)

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, which is not supported
 3: you are stubbing the behaviour of another mock inside before 'thenReturn' instruction if completed
at org.opensaml.xmlsec.signature.impl.SignatureImpl.getXMLSignature(SignatureImpl.java:153)

【问题讨论】:

    标签: java unit-testing exception mockito stubbing


    【解决方案1】:

    SignatureValidator.validate(...)是静态方法,不是实例方法;所以有问题的陈述:

    doThrow(SignatureException.class).when(mockValidator).validate(mockSignature,mockCredential);
    

    等价于:

    doThrow(SignatureException.class).when(mockValidator);
    SignatureValidator.validate(mockSignature,mockCredential);
    

    我想你可以明白为什么这是“未完成的存根”。

    (不幸的是,Java 甚至允许您编写 <i>instance</i>.<i>staticMethod</i>(...) 而不是 <i>ClassName</i>.<i>staticMethod</i>(...),因为前者非常具有误导性。一些编译器会警告您。)

    有关如何模拟静态方法的信息,请参阅 Stack Overflow 问题:Mocking static methods with Mockito

    【讨论】:

      猜你喜欢
      • 2018-02-01
      • 1970-01-01
      • 2021-07-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多