Java Mocking Frameworks
Today, One of the major challenges any software developers face when writing unit tests is how to handle external dependencies. In order to execute a unit test, you may need a open live connection to a fully populated database, or some remote server; or perhaps there is a need to instantiate a complex class created by someone else.
Every now and then, the any software code under unit testing executes faultlessly but then you need to set up the environment that the unit testing be contingent on. This dependence makes your unit testing very hard and they might fail. If unit tests from time to time fail, depending on the environment, you won’t trust them, which defeats the point.
The key mechanism for solving these kinds of problems lies in the procedure of mocking. The mocking mechanism replaces the production code behaviour (like the database calls), with software code that we can control.
The first mocking experience usually starts out with hand-rolled mocks. These are classes that are coded by the developer and can replace the production objects for the purpose of testing.
Creating your very first mock is simple – all you need is a class that looks like the class you’re replacing, which can be done using inheritance (in Object Oriented languages).
What is Mocking Frameworks?
Mock frameworks are just simply a software tools. Sure you could do it manually, but it is time consuming process for maintaining classes that are just for testing. It is used to help for defining the fake objects, but without the coding.
Mocking frameworks perform three main functions:
- Create fake objects
- Set behavior on fake objects
- Verify methods were called
Mocking Framework Types
Basically there are three types of mocking frameworks. They are
- Some create a fake object at run-time,
- Others generate the needed code during compilation, and
- Yet others use method interception to catch calls to real objects and replace these with calls to a fake object.
Java Mocking Frameworks
The number of mocking frameworks has proliferated in recent years. A good mock framework can make writing unit tests a breeze. A bad mock framework can deter even the most conscientious of developers. When writing the tests takes two or three times longer than writing the actual code itself, well, that’s just too much work to do everyday.
PowerMock is a framework that extend other mock libraries such as EasyMock with more powerful capabilities. PowerMock uses a custom classloader and bytecode manipulation.
PowerMock in Java allows you to mock any static methods, constructors, final classes and methods, private methods, removal of static initializers and more.
The ease of Ruby for scripting tasks makes it a very powerful candidate for writing your Test suites. Until recently, there was no real standalone framework to test your Java with Ruby. JtestR written by Ola Blini (a member of JRuby team) and Anda Abramovici makes it possible now.
Easy mocking. Better testing. EasyMock is a mock framework which can be easily used in conjunction with JUnit. EasyMock has several methods which are used to configure the Mock object.
Mockito is a mocking framework. Mockito library enables mocks creation, verification and stubbing. It lets you write beautiful tests with clean & simple API. Mockito doesn’t give you hangover because the tests are very readable and they produce clean verification errors.
finalmethods, constructors, and future instances
finalclasses, enums, annotations, and multiple interfaces
- mocks implementation classes given just the interface or base class
- expressive recording & verification API, with elegant and consistent syntax
- concise argument matchers and argument capturing
- dependency injection for both unit and integration tests
Advantages of Mock Frameworks over Hand-Rolled Mocks
- The mock behavior is closer to the test code because it is in the test code. This makes tests easier to understand without having to look at other code.
- You don’t need to create yet another mock class (or worse: add logic to an existing one) just because you want slightly different behavior.
- You end up writing less boilerplate because you only need to setup the methods you want to mock, while with hand-rolled ones you have to implement the whole interface.
Disadvantages of Mock Frameworks over Hand-Rolled Mocks
- You have to learn a framework, while anyone can write mocks by hand.
- It is yet another dependency you add to your project.