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!
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.
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.