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

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

Closed
JavaDevaraj opened this issue Feb 28, 2025 · 4 comments
Closed

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

JavaDevaraj opened this issue Feb 28, 2025 · 4 comments
Labels
in: test Issues in the test module

Comments

@JavaDevaraj
Copy link

JavaDevaraj commented Feb 28, 2025

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!
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Feb 28, 2025
@JavaDevaraj JavaDevaraj 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 Feb 28, 2025
@bclozel bclozel added the in: test Issues in the test module label Feb 28, 2025
@sbrannen sbrannen self-assigned this Feb 28, 2025
@sbrannen sbrannen 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 Feb 28, 2025
@sbrannen
Copy link
Member

sbrannen commented Mar 1, 2025

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

@sbrannen sbrannen added the status: waiting-for-feedback We need additional information before we can continue label Mar 1, 2025
@quaff
Copy link
Contributor

quaff commented Mar 3, 2025

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

see #33935

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Mar 3, 2025
@bclozel bclozel added status: waiting-for-feedback We need additional information before we can continue and removed status: feedback-provided Feedback has been provided labels Mar 3, 2025
@sbrannen sbrannen removed their assignment Mar 5, 2025
@spring-projects-issues
Copy link
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 added the status: feedback-reminder We've sent a reminder that we need additional information before we can continue label Mar 10, 2025
@spring-projects-issues
Copy link
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.

@spring-projects-issues spring-projects-issues closed this as not planned Won't fix, can't repro, duplicate, stale Mar 17, 2025
@spring-projects-issues spring-projects-issues removed status: waiting-for-feedback We need additional information before we can continue status: feedback-reminder We've sent a reminder that we need additional information before we can continue status: waiting-for-triage An issue we've not yet triaged or decided on labels Mar 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: test Issues in the test module
Projects
None yet
Development

No branches or pull requests

5 participants