File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Testing and the fly likes What exactly is the point of mock objects (I don't understand them)? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Testing
Bookmark "What exactly is the point of mock objects (I don Watch "What exactly is the point of mock objects (I don New topic
Author

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

Benjamin Weaver
Ranch Hand

Joined: Apr 08, 2003
Posts: 161
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!
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30537
    
150

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Benjamin Weaver
Ranch Hand

Joined: Apr 08, 2003
Posts: 161
Thanks, Jeanne, this helps.

I will try out some mock objects soon.
enric jaen
Greenhorn

Joined: Oct 15, 2010
Posts: 25
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.
 
Don't get me started about those stupid light bulbs.
 
subject: What exactly is the point of mock objects (I don't understand them)?