-
Notifications
You must be signed in to change notification settings - Fork 38.7k
Not planned
Labels
in: testIssues in the test moduleIssues in the test module
Description
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!
Metadata
Metadata
Assignees
Labels
in: testIssues in the test moduleIssues in the test module
Type
Projects
Milestone
Relationships
Development
Select code repository
Activity
[-]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[/+][-]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[/+]sbrannen commentedon Mar 1, 2025
Hi @JavaDevaraj,
That exception can occur when
reprocessBackOffImpl
is eithernull
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.
@MockitoBean
in@Configuration
classes #33934MockReset
strategy is no longer honored for@MockitoBean
and@MockitoSpyBean
#33941@MockitoBean
is not supported in@Component
class like@MockBean
is #34415Unfortunately, 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 withdoThrow()
.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 commentedon 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 commentedon Mar 10, 2025
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 commentedon Mar 17, 2025
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.
@MockitoBean
is not properly initialized in Kotlin test class with Spring Boot 3.4.4 #34832