Javatpoint Logo
Javatpoint Logo

Mockito vs. EasyMock

Mockito is an open-source, Java-based mocking framework used in unit testing. The Mockito framework is released under the MIT (Massachusetts Institute of Technology) License. It allows the creation of mock objects in automated unit tests for the purpose of test-driven development (TDD) or behavior-driven development (BDD). It is used along with other testing frameworks such as TestNG and JUnit.

EasyMock is an open-source, Java-based testing framework used for testing Java applications. The EasyMock framework is released under the Apache License. It allows the creation of mock objects of a given interface by using Java Reflection. Like Mockito and EasyMock, it is also used in conjunction with other testing frameworks such as TestNG and JUnit.

  • Stubs: Stubs are the objects that hold predefined data. It acts as a temporary object for a called module and gives the same output as the original software.
  • Mocks: Mocks are the clone or dummy objects that simulate the behavior of the real objects.
  • Spies: Spies are the partial mock objects of the real one.

On the other hand, EasyMock is an open-source, Java-based testing framework used for testing Java applications. Like Mockito, EasyMock mock both classes and interfaces. It allows dynamic (at runtime) creation of mock objects for a given interface by using Java Reflection API. The EasyMock framework is released under the Apache License.

We cannot mock final and private methods by using EasyMock. Also, it provides build-in behavior for toString(), hashCode(), equal(), and finalize() methods. Like Mockito, EasyMock can also be used in conjunction with other testing frameworks such as TestNG and JUnit.

Following are some important differences between Mockito and EasyMock:

Parameters Mockito EasyMock
License Mockito framework was released under the MIT (Massachusetts Institute of Technology) License. The EasyMock framework was released under the Apache License.
Support test spies Mockito supports both mocks as well as spies. Both spies and mocks perform different functions. Spy creates a partial mock object, whereas mock creates a dummy/ fake (fully mock) object of the real one. Whereas EasyMock supports only mocks. It does not support spies.
Mocked method calls In Mockito, we use Mockito.when(mock.method(args)).thenReturn(value) method for mocking a method calls. In EasyMock, the EasyMock.expect(mock.method(args)).andReturn(Value) method for mocking a method call.
Verifying calls In Mockito, the Mockito.verify(mock).method(args) is used for verifying calls to a mock. In EasyMock, the EasyMock.verify(mock) is used for verifying calls to a mock, but this method is always used after calling the EasyMock.replay(mock) method.
Exception Throwing In Mockito, throwing exception can be mocked using .thenThrow(ExceptionClass.class) after calling the Mockito.when(mock.method(args)) method. In EasyMock, throwing exception can be mocked using .andThrow(new ExceptionClass()) after calling the EasyMock.expect(..) method.

Mockito is the most popular mocking framework used for testing Java applications. It is better than any other testing and mocking framework, such as EasyMock.






Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA