Difference between Stub and Mock object in Java Unit testing - JUnit

JUnit is the most popular framework for unit testing Java code. Unit testing is used to test a single programming unit e.g. a class or a method, in-fact many Java developers write unit tests on a per-method basis. Stub and Mock objects are two concepts that help during unit testing, they are not real objects but act like real objects for unit testing purposes. By the way, If you are absolutely a beginner in Java Unit testing then you can see this post to learn how to create the JUnit test in Eclipse. Coming back to Stubs and Mocks, One reason for using Stub and Mock object is the dependency of one unit into another. Since in real-world, the most unit doesn't work in isolation, they use dependent class and object to complete there task.

One of the common testing approaches to test a unit, which depends on the other units is by using Stubs and Mock objects. They help to reduce complexity, which may be required to create an actual dependent object.

In this tutorial, we will learn a few basic differences between Stub and Mock objects in Java Unit testing. This post is rather small to tackle this whole topic, at best it just provides an introduction. I would suggest following some good books on Java Unit testing like Pragmatic Unit Testing in Java.

This is one of the must-read books in Java Unit testing, and my personal favorite as well. Apart from teaching the basics of Unit testing it also gives a nice overview of Mock objects and mock framework.

Actually, from there, I came to know about EasyMock and jMock frameworks and how useful they can be. Even if you have been using JUnit and unit testing, you can learn a lot from this book. It's like learning from basics to best practices of Unit testingMockito is another useful library that enables mocks creation, verification, and stubbing.






Stub vs Mock Objects in Java testing

As I said both Stub and Mock are dummy object, but more precisely, a Stub is an object that simulates real objects with the minimum number of methods required for a test. For example, if your class is dependent upon the database, you can use HashMap to simulate database operation. Stub object is mostly created by developers and their method is implemented in predetermined way, they mostly return hardcoded values.

They also provide methods to verify methods to access stub's internal state, which is necessary to verify the internal state of the object. On the other Mock objects are usually created by open-source libraries and mock frameworks like MockitojMock and EasyMock. These library helps to create, verify, and stub mocks. Mock object has knowledge of how it's methods are executed in a unit test, and it verifies if methods are actually get called against the expected ones.

Apart from first difference that mock objects are usually created by mock framework, another key difference between Stub and Mock object is how they are used.  Stub object is usually used for state verification, while the mock object is mostly used for behaviour verification. As a Java developer, you can take advantage of these powerful testing techniques to test your module in isolation.




That's all on the difference between Stub and Mock object in Java Unit testing. Though not many Java developers use Mock objects, or even unit tests, which is one of the best development practice along with code review. I always suggest writing as many unit tests as possible, this not only help you to write production-quality code but also improves your knowledge of a particular module in your project. 

For further reading, you can see Pragmatic JUnit testing in Java, one of the best books on Java Unit testing, and id you need some active learning then you can also join these best JUnit and Mockito courses for Java Developers. 

4 comments :

Anonymous said...

Good Post, Knowledge of Stub and Mock is absolutely must in Test driven development. It's mandatory for all Java TDD practitioner to get familiar with JUnit and one of the mock testing framework e.g. Mockito.

BlueOcean said...

Martin Fowler's blog back to 2007 has very good explanation on this topic. Yep, this is a well settled topic. Stub requires we program to interface not to class, so developer can write stub based on interface. Mock is proxy objects, allowing developers to write unit tests without testing other dependency classes.

Anonymous said...

Mockito Mock objects are much more intelligent then Stub created by yourself. It is capable to respond differently to different method call regardless of argument. For example, its possible that when you first call a method it return one output while in second call it return a different output. Mockito mocks can even record the method calls and allows you to assert that whether calls were actually made and expected number of times.

Gautam said...

I thing most significant difference between Mock and Stub is that, You can finish your testing with very less code in case of Mock because framework like Mockito will generate code for you, for stub you need to write lot of code to create your own implementation. To be frank, only because of this reason I prefer Mock over Stub. It lets you create unit test faster.

Post a Comment