Saturday, May 26, 2012

JMockit Tutorial: How to add JMockit to an eclipse project?

Download JMockit:

Get the latest release from here. At the time of writing this blog the latest version is jmockit-0.999.15.You must have a zip named Prerequisites - JDK should be 1.6+  and Junit 4.5+ 

Extract JMockit into a folder:

Unzip the contents of the zip file into a folder, say C:\Documents\jmockit. The jmockit folder should look like this with all the jars of jmockit.

 Add jmockit.jar into project's library

Next step is pretty straight forward if you know how to add an external jar into eclipse. Right click on the project in Package Explorer --> Build Path --> Configure Build Path .See the screen shot below.

 Click on the Libraries tab --> Add External JARS. Browser the folder in which you have stored jmockit.jar and add it. It will be listed in the window opened.

And thats it! Now we can use the JMockit constructs into our project and start mocking objects.
But before you run a test that uses JMockit, we need to add the java agent to the test class we need to run. You can read about it here


  1. Instead of adding the "-javaagent" parameter, go to the "Order and Export" tab and move jmockit.jar above junit.jar. Nothing else needed. (With TestNG 6.2+, even this step is not necessary, if using JDK 1.6+.)

    1. Wow! You made my day! JMockit's creator himself visting my blog!
      My encounter with jmockit started when I started interning with IBM. I faced a lot of problems in making it compatible with IBM's jdk.

      I will update my blog once I try it out myself..