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.

Unable to verify spy behavior when spying on proxy

See original GitHub issue

When spying on a proxy instance (discovered with springs @Transactional), Mockito’s verify(mock) method, will nearly always return successful even when the expectations are absurd.

I wrote this test to demonstrate the unexpected behavior: Test of Spying on Proxy

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:11 (4 by maintainers)

github_iconTop GitHub Comments

13reactions
bric3commented, Aug 13, 2016

So instead of : spy(proxy) use mock(TestSubject.class, delegatesTo(springProxy)).

However if annotations are need to read, then you’ll need mockito 2 beta. Because mockito 1.x uses CGLIB which doesn’t copies annotations on the mockito subclass. Mockito 2 uses the great bytebuddy.

1reaction
praveenk007commented, Apr 16, 2019

So instead of : spy(proxy) use mock(TestSubject.class, delegatesTo(springProxy)).

However if annotations are need to read, then you’ll need mockito 2 beta. Because mockito 1.x uses CGLIB which doesn’t copies annotations on the mockito subclass. Mockito 2 uses the great bytebuddy.

This doesnt seem to work. I am using below powermockito version

compile group: 'org.powermock', name: 'powermock-module-junit4', version: '2.0.0'
compile group: 'org.powermock', name: 'powermock-api-mockito2', version: '2.0.0'
Read more comments on GitHub >

github_iconTop Results From Across the Web

Mockito - spying on real objects calls original method
In this case Mockito proxy is not being executed. I would like to keep existing CGLIB decoration and still be able to use...
Read more >
Mockito - Using Spies - Baeldung
In this tutorial, we'll illustrate how to make the most out of spies in Mockito. We'll talk about the @Spy annotation and how...
Read more >
Mockito' spy() method and Spring - A Java geek
This magic method creates a proxy (hence the name spy) on a real object. It delegates its method calls to the proxied object...
Read more >
Mockito 4.1.0 API - javadoc.io
Spying on real objects. You can create spies of real objects. When you use the spy then the real methods are called (unless...
Read more >
Top 12 Mockito Interview Questions (Mocking Framework ...
Answer: Spy is a type of partial mock supported by Mockito. ... But it still allows you to verify the interactions with the...
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