Saturday, 7 June 2014

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 developer write unit test on per method basis. Stub and Mock objects are two concepts which helps during unit testing, they are not real object, but act like real object for unit testing purpose. By the way, If you are absolutely beginner in Java Unit testing then you can see this post to learn how to create JUnit test in Eclipse. Coming back to Stubs and Mocks, One reason of using Stub and Mock object is dependency of one unit into another. Since in real world, most unit doesn't work in isolation, they use dependent class and object to complete there task. One of the common testing approach to test a unit, which depends on other unit is by using Stubs and Mock objects. They help to reduce complexity, which may be require to create actual dependent object. In this tutorial, we will learn few basic difference between Stub and Mock object in Java Unit testing. This post is rather small to tackle this whole topic, at best it just provide an introduction. I would suggest to follow on some good books on Java Unit testing e.g. Pragmatic Unit Testing in Java. This is one of the must read book in Java Unit testing, and my personal favourite as well. Apart from teaching 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 form this book. It's like learning from basics to best practices of Unit testingMockito is another useful library 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 database, you can use HashMap to simulate database operation. Stub object is mostly created by developers and there method is implemented in predetermined way, they mostly return hard coded values. They also provide methods to verify methods to access stub's internal state, which is necessary to verify internal state of object. On the other Mock objects are usually created by open source library and mock framework 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 mock object is mostly used for behaviour verification. As a Java developer, you can take advantage of this powerful testing techniques to test your module in isolation.

That's all on difference between Stub and Mock object in Java Unit testing. Though not many Java developers uses Mock objects, or even unit test, which is one of the best development practice along with code review. I always suggest to write as many unit test 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 book on Java Unit testing.

No comments:

Post a Comment