Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

verify() on a wrapped mock fails with UnfinishedVerificationException #2235

Closed
mschieder opened this issue Mar 22, 2021 · 0 comments
Closed

Comments

@mschieder
Copy link
Contributor

I am currently using the Mock resolver plugin feature (#2042) in my projects, but verify() on a wrapped mock does not work as expected and throws a UnfinishedVerificationException.

A junit5 test case to reproduce this bug is already in the extTest module: MockResolverTest but it is not executed by default.

@Test
void mock_resolver_can_unwrap_mocked_instance() {
Foo mock = mock(Foo.class), wrapper = new MockWrapper(mock);
when(wrapper.doIt()).thenReturn(123);
assertThat(mock.doIt()).isEqualTo(123);
assertThat(wrapper.doIt()).isEqualTo(123);
verify(wrapper, times(2)).doIt();
}

After adding the junitJupiterEngine as test runtime, the MockResolverTest is executed, but fails with following message:

Missing method call for verify(mock) here:
-> at org.mockitousage.plugins.resolver.MockResolverTest$MockWrapper.doIt(MockResolverTest.java:47)

Example of correct verification:
    verify(mock).doSomething()

Also, this error might show up because you verify either of: final/private/equals()/hashCode() methods.
Those methods *cannot* be stubbed/verified.
Mocking methods declared on non-public parent classes is not supported.

org.mockito.exceptions.misusing.UnfinishedVerificationException: 
Missing method call for verify(mock) here:
-> at org.mockitousage.plugins.resolver.MockResolverTest$MockWrapper.doIt(MockResolverTest.java:47)

Example of correct verification:
    verify(mock).doSomething()

Also, this error might show up because you verify either of: final/private/equals()/hashCode() methods.
Those methods *cannot* be stubbed/verified.
Mocking methods declared on non-public parent classes is not supported.

	at org.mockito.internal.exceptions.Reporter.unfinishedVerificationException(Reporter.java:115)
	at org.mockito.internal.progress.MockingProgressImpl.validateMostStuff(MockingProgressImpl.java:124)
	at org.mockito.internal.progress.MockingProgressImpl.validateState(MockingProgressImpl.java:105)
	at org.mockito.internal.MockitoCore.validateMockitoUsage(MockitoCore.java:248)
	at org.mockito.Mockito.validateMockitoUsage(Mockito.java:3248)
	at org.mockito.internal.framework.DefaultMockitoSession.finishMocking(DefaultMockitoSession.java:93)
	at org.mockito.junit.jupiter.MockitoExtension.afterEach(MockitoExtension.java:186)
	at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeAfterEachCallbacks$11(TestMethodTestDescriptor.java:253)
	at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeAllAfterMethodsOrCallbacks$12(TestMethodTestDescriptor.java:269)
	at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
...

The reason why failing is a missing MockUtil.resolve() before the identity check here:

if (((MockAwareVerificationMode) verificationMode).getMock() == invocation.getMock()) {

I will provide a PR to fix this issue.

mschieder added a commit to mschieder/mockito that referenced this issue Mar 22, 2021
mschieder added a commit to mschieder/mockito that referenced this issue Mar 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant