Skip to content

@SpyBean can be stubbed, but @MockitoSpyBean cannot be stubbed #34516

@JavaDevaraj

Description

@JavaDevaraj

In Spring Boot 3.3:

@SpyBean
private ReprocessBackOffImpl reprocessBackOffImpl;
doThrow(exception).when(reprocessBackOffImpl).assertPreConditions(context);

It's working fine....

but after upgrading to Spring Boot 3.4:

@MockitoSpyBean
private ReprocessBackOffImpl reprocessBackOffImpl;
doThrow(exception).when(reprocessBackOffImpl).assertPreConditions(context);

I'm getting the following exception.

Argument passed to when() is not a mock!
Example of correct stubbing:
    doThrow(new RuntimeException()).when(mock).someMethod();
org.mockito.exceptions.misusing.NotAMockException: 
Argument passed to when() is not a mock!

Activity

changed the title [-]In SpringBoot 3.3 @SpyBean can able to mock and spy but @MockitoSpyBean can able to spy but cannot able to mock[/-] [+]In SpringBoot 3.3 @SpyBean can able to mock and spy but after upgrading to Springboot 3.4 @MockitoSpyBean can able to spy but cannot able to mock[/+] on Feb 28, 2025
self-assigned this
on Feb 28, 2025
changed the title [-]In SpringBoot 3.3 @SpyBean can able to mock and spy but after upgrading to Springboot 3.4 @MockitoSpyBean can able to spy but cannot able to mock[/-] [+]`@SpyBean` can be stubbed, but `@MockitoSpyBean` cannot be stubbed[/+] on Feb 28, 2025
sbrannen

sbrannen commented on Mar 1, 2025

@sbrannen
Member

Hi @JavaDevaraj,

That exception can occur when reprocessBackOffImpl is either null or not a mock/spy.

If it's null, that's probably because you are using @MockitoSpyBean on a field somewhere other than in a test class.

If it's non-null and still not a mock/spy, that would could be due to a bug (which may have already been fixed).

In light of the above, your issue could be a duplicate of one of the following.

Unfortunately, the examples you have provided do not provide any context. We have no way of inferring where you declared the @MockitoSpyBean field or where you attempted to stub it with doThrow().

Please review the aforementioned issues to see if one of those addresses your issue.

If you wish for us to investigate this further, please provide a simple application that demonstrates the issue (preferably something that we can download and run such as a an application in a public Git repository or a ZIP file attached to this issue).

Regards,

Sam

quaff

quaff commented on Mar 3, 2025

@quaff
Contributor

@JavaDevaraj Please note @MockitoSpyBean will not create bean instance if existing bean not found while @SpyBean does.

see #33935

removed their assignment
on Mar 5, 2025
spring-projects-issues

spring-projects-issues commented on Mar 10, 2025

@spring-projects-issues
Collaborator

If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.

spring-projects-issues

spring-projects-issues commented on Mar 17, 2025

@spring-projects-issues
Collaborator

Closing due to lack of requested feedback. If you would like us to look at this issue, please provide the requested information and we will re-open the issue.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    in: testIssues in the test module

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @bclozel@sbrannen@quaff@spring-projects-issues@JavaDevaraj

        Issue actions

          `@SpyBean` can be stubbed, but `@MockitoSpyBean` cannot be stubbed · Issue #34516 · spring-projects/spring-framework