简体   繁体   中英

I am trying to mock a final java class using Mockito 2, class is getting mocked but I still get Unfinished Stubbing Exception

I have included MockMaker file as well- src\test\resources\mockito-extensions\org.mockito.plugins.MockMaker

The related code is as shown where SignatureValidator is final class-

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);
    }

Stack Trace-

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(...) is a static method, not an instance method; so the statement in question:

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

is equivalent to this:

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

and I think you can see why that's "unfinished stubbing".

(It's unfortunate that Java even lets you write instance . staticMethod (...) instead of ClassName . staticMethod (...) , since the former is so misleading. Some compilers will warn you about this.)

For information about how to mock static methods, see this Stack Overflow question: Mocking static methods with Mockito .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM