EasyMock Tutorial With Examples

EasyMock is a java based mocking framework, which is used in conjunction with other unit testing frameworks such as JUnit and TestNG. EasyMock is very similar to Mockito mocking framework.

EasyMock allows us to create mock objects of interfaces and concrete classes and specify their behaviors. Then we can use these mocks to write code to test our main classes. This way writing unit tests are simplified as we don’t need to care about external dependencies.

EasyMock

We can use EasyMock to create mock objects and then inject them into the classes we want to test. However, EasyMock has certain limitations too.

  • EasyMock can’t mock final and private methods. If these methods are called on mock object, then normal methods will get called.
  • EasyMock provides built-in behavior for equals(), toString(), hashCode() and finalize() methods. It means that we can’t record our own behavior for these methods.

EasyMock Maven Dependencies

We can add the following dependencies to our maven project to use the EasyMock mocking framework.

EasyMock Tutorial

Let’s create some sample classes and services for mocking. We will use JUnit 5 for writing test cases along with EasyMock for creating mock objects.

Note that we don’t need to have Calculator implementation for writing our test cases because we are mocking them. This way we can apply the Test-Driven-Development (TDD) model in our application.

Most of the useful methods are provided in the org.easymock.EasyMock class. Since they are mostly static, we can import them to write fluent and more readable code.

We can also mock a concrete class and specify its behaviors. Let’s first look at a simple example, where we will mock ArrayList class.

Some important points to note from above EasyMock example are:

  • First step is to create mock object using EasyMock mock() method. This method is overloaded where we can specify if we want our mock object to be nice or strict. We can also specify name to be used incase of logging exceptions. We will look into different MockType examples in future posts.
  • Next step is to stub the behavior and output of a method call using expect() and andReturn() chain statements.
  • Finally, we have to call replay() method to start using stub methods on mock objects.
  • If we call a method that is not stubbed, we will get java.lang.AssertionError.

EasyMock Annotations – @Mock, @TestSubject

EasyMock also provides few annotations to create mock objects.

  • @Mock: Used to specify a field to be mocked by EasyMock.
  • @TestSubject: Used with a field so that EasyMock will inject mocks created with @Mock on its fields.

When using annotations, we have to use EasyMockRunner, EasyMockRule or EasyMockSupport.injectMocks(Object) to initialize and inject mock objects. Since we are using JUnit-5, we can’t use EasyMockRunner and EasyMockRule as they are not supported yet. So we will have to use EasyMockSupport.injectMocks(Object). We can call this method in @BeforeEach method.

Here is the complete example of mocking objects using EasyMock annotations.

EasyMock Iterator Style Stubbing

We can specify different behaviors for the same method calls by chaining them with times(). Let’s look at a simple example to understand this clearly.

Summary

EasyMock is a simple tool to create mock objects and write unit tests easily. It’s very similar to Mockito and provides almost the same features as Mockito.

You can checkout complete project and more EasyMock examples from our GitHub Repository.

Reference: Official User Guide

By admin

Leave a Reply

%d bloggers like this: