Mocking session scoped spy bean calls real method
See original GitHub issueTrying to mock a @SessionScope
in a test by autowiring it using @SpyBean
and then mocking using doReturn(..).when(sessionScopedBean).method(any())
calls the real method with null argument instead of silently registering mock.
Removing @SessionScope
from the bean resolves this issue.
Spring boot 2.1.6.RELEASE
Issue Analytics
- State:
- Created 4 years ago
- Comments:13 (12 by maintainers)
Top Results From Across the Web
How to Mock, Spy, and Fake Spring Beans - DZone
Today I'm introducing a better way to spy/mock Spring Beans based on the @Profile annotation. This blog post is going to describe this ......
Read more >Mockito @Spy calls real method while mocking behaviour is ...
This seems to work as when I debug in my Controller the keycloakUserRepresentation has the correct values. It worked because the bean wasn't ......
Read more >Mocking CDI beans in Quarkus
Starting with Quarkus 1.4 , users have the ability to create and inject per-test mocks for normal scoped CDI beans using io.quarkus.test.junit.
Read more >Core Features - Spring
This section dives into the details of Spring Boot. Here you can learn about the key features that you may want to use...
Read more >Difference between Spy and Mock thenCallRealMethod
As we can see we called the actual implementation for getNoOfWorkingDays() and getSalaryPerDay() and mocked the processPay() method. So similar partial mocking ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
We’ve already made the only “fix” that we can, and that is to add a note to the documentation about
mockito-inline
.I did some debugging. Seems like that
@SessionScope
is enhanced by Spring so every method in it becomesfinal
, thus Mockito can’t enhance it again. I’m wondering, why does Spring make all methods final when enhancing the bean? Can we somehow only make the injected methods final, but keep all declared methods non-final?