gpt4 book ai didi

java - 我正在尝试使用 Mockito 2 模拟最终的 java 类,类被模拟,但我仍然收到未完成的 stub 异常

转载 作者:行者123 更新时间:2023-12-02 08:47:10 27 4
gpt4 key购买 nike

我也包含了 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)

最佳答案

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

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

相当于:

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

我想你应该明白为什么这是“未完成的 stub ”。

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

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

关于java - 我正在尝试使用 Mockito 2 模拟最终的 java 类,类被模拟,但我仍然收到未完成的 stub 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61005537/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com