• 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

What exactly is the point of mock objects (I don't understand them)?

 
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to understand how to use Mockito for testing.

I understand JUnit. Among other things you can instantiate a dummy object of a class you want to test and perform various assertions on its methods and functionality.

But if you have already the class written you want to test, what is the point of using a mock object (e.g. Mockito) as opposed to a dummy object? What can you do with a mock object that you can't do with a dummy object? Is it just a matter of convenience? that you don't have to insert dummy values into your dummy object, using instead, for example, the 'when' logic on a Mockito mock object?

I am a newbie and trying to understand. All suggestions and comments are welcome!
 
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
Mock objects are faster to use than writing dummy objects. For example, suppose I call a web service that has 100 methods. To mock it out, I need about 10 lines of code. For a dummy object, I need to "implement" 100 methods.
 
Benjamin Weaver
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, Jeanne, this helps.

I will try out some mock objects soon.
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
first, I am not sure in your post what you mean for dummy? Are you refering to the system under test (SUT), or an object that the SUT depends on?

if you mean the second, advantages of mock obkects over dummy object (AKA fake object) are:

- if you SUT depends on a final class, you can't create a dummy of a final class, but you can create a mock.

- if your need to implement several methods of the dummy object , you may end up implementing production code. With a mock, you just tell what you expect of each method, but you don't need to implement code.

- if the class you need to fake has a complex constructor (i.e. depends on other classes), when you write a dummy that inherits that class the constructor will be called. With a mock object not.

- you can't write a dummy object that overrides static methods. With mock objects you can.
 
I'm still in control here. LOOK at this tiny ad!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic