Skip to content

Reject attempt to use @MockitoSpyBean with a scoped proxy #35722

Description

Stubbing method calls on a @MockitoSpyBean in conjunction with ScopedProxyMode.TARGET_CLASS results in java.lang.IllegalStateException: Failed to unwrap proxied object.

Steps to reproduce:

  1. Write a Spring bean that uses @Scope(proxyMode = ScopedProxyMode.TARGET_CLASS).
  2. In an integration test, make it a spy bean.
  3. Mock a call to the spy bean.
  4. Run the test.

I think the issue could be in org.springframework.test.context.bean.override.mockito.SpringMockResolver.getUltimateTargetObject. It invokes the method org.springframework.aop.framework.Advised#getTargetSource() on the spy bean and is (indirectly) called by org.mockito.internal.stubbing.StubberImpl#when(T mock), which means when(T mock) is not finished when getTargetSource() is called.

Please find the attached reproducer which has a full setup. Slightly more detailed information can be found in the contained README.md.

spy-beans.zip

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions