• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Spy In Mocking

 
Ranch Hand
Posts: 157
Netbeans IDE Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

I need to discuss about spy objects in Mockito. As I red Its provide the partial mocking. And If we want to invoke the actual method implementation we use spy. But If we using mocking then what is the need to invoke method actually, we can define the behavior using when..thenReturn. Also if there could be short demo example please provide.

Thanks
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Vivek,
A spy is used when you want to partially mock the object. (I almost never have need for a spy. Usually the temptation flags the fact that my class is doing too much or at different levels of abstraction.) Also, if I have one small thing to mock, I'll make it a method and create a subclass with a dummy value or dummy logic. More like a stub than a spy.

That said, here's an example.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic