Skip to content

Stubbing @MockitoSpyBean with ScopedProxyMode.TARGET_CLASS fails #35722

@i-am-not-giving-my-name-to-a-machine

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

Metadata

Metadata

Assignees

Labels

in: testIssues in the test modulestatus: waiting-for-triageAn issue we've not yet triaged or decided on

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions