EasyMock gets more power with PowerMock.

There are a lot of mocking libraries for Java, out of which EasyMock is my favorite. It has been serving well for me until I had to unit test legacy code which did not completely adhere to IOC (inversion of control) principles and had a lot of static method calls and direct creation of service/helper objects instead of using dependency injection. Unfortunately EasyMock could not handle mocking of static methods and objects which were created directly in the class or method being tested along with some other limitations such handling final classes.
This forced me to use other mocking libraries until I encountered PowerMock. It provides all the ammunition (and more) in dealing with these issues and has nice integration with EasyMock.
Here I’ll show you how to use EasyMock with PowerMock in order to handle common mocking test cases which were not possible with EasyMock before.

Set Up.

First to use PowerMock with easymock you need PowerMock libraries.
Here is maven dependency list:

<dependency>
        <groupId>org.powermock</groupId>
        <artifactId>powermock-module-junit4</artifactId>
        <version>1.4.10</version>
        <scope>test</scope>
</dependency>
<dependency>
        <groupId>org.powermock</groupId>
        <artifactId>powermock-module-test-easymock-junit4</artifactId>
        <version>1.4.10</version>
        <scope>test</scope>
</dependency>
<dependency>
        <groupId>org.easymock</groupId>
        <artifactId>easymock</artifactId>
        <version>3.0</version>
        <scope>test</scope>
</dependency>

Code to be tested.

The Unit Tests.

I am creating 5 different tests in order to gradually show you how to test different scenarios with last one combining all into one test as an example of how everything can work together.

Update: Just encountered limitation with PowerMock (and probably with any other Mocking frameworks) that it cannot mock “java.lang.reflect.Method”. The workaround is to just declare inner class that has method you need to mock.

3 comments to EasyMock gets more power with PowerMock.

  • shalvi

    You need to use the following dependencies in order to make your code working. Thought would share so that people searching the similar code would be benifitted

    org.powermock
    powermock-module-junit4
    1.6.2

    org.powermock
    powermock-module-test-easymock-junit4
    1.6.2

    org.easymock
    easymock
    3.0

    javassist
    javassist
    3.12.1.GA

  • shalvi

    org.powermock
    powermock-module-junit4
    1.6.2

    org.powermock
    powermock-module-test-easymock-junit4
    1.6.2

    org.easymock
    easymock
    3.0

    javassist
    javassist
    3.12.1.GA

  • Nikhil Apte

    Fantastic post. Was very helpful for me to get started with powermock-easymock. Thank you very much !!

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>