question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Mocking session scoped spy bean calls real method

See original GitHub issue

Trying 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:closed
  • Created 4 years ago
  • Comments:13 (12 by maintainers)

github_iconTop GitHub Comments

1reaction
wilkinsonacommented, Mar 9, 2020

We’ve already made the only “fix” that we can, and that is to add a note to the documentation about mockito-inline.

1reaction
blindpiratecommented, Aug 11, 2019

I did some debugging. Seems like that @SessionScope is enhanced by Spring so every method in it becomes final, 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?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found