Difference between Stub and Mock object in Java Unit testing - JUnit (original) (raw)
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 testing. Mockito 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 Mockito, jMock 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.