Unit Testing with TestNG and jmockit
TestNG is a testing framework for unit test development. jmockit is a framework for mock objects that provides mock object functionality using the java.lang.instrument package of jdk 1.5. Together, these frameworks can provide the tools to create very robust test cases without design limitations of other testing frameworks currently available. In part one of this two part tutorial, we will cover the creation of a test case and the implementation of the related class to be tested. We will reuse the same scenario as in my tutorial: Unit testing with JUnit and EasyMock. If you are new to unit testing in general, I suggest you check out the JUnit tutorial as well for it's section on unit testing in general. In part two, we will cover scenarios that favor the TestNG and jmockit frameworks including testing using mocks without injection and organizing tests into groups.

To begin, complete my Unit testing with JUnit and EasyMock tutorial. Then run TestNG's JUnit converter. That's it. Tutorial over. Just kidding (although TestNG does have a converter that you can run to convert existing JUnit tests to TestNG tests).


Although TestNG does provide a JUnit conversion tool, that is not the approach we will take here. So let's get started. I will be using Eclipse 3.3 Europa to do this tutorial. To begin, create a new java project and call it TestNGTutorial. Right click on your new project and select New --> Folder. Name it lib and click Finish. Usually you don't want to package your test code with your regular code, so let's make an additional source directory, test. To do that, right click on your new project and select Source Folder. Next we need to add TestNG to our build path. To make our life easier and since we are serious about unit testing ;) we will use the TestNG plugin for Eclipse. It includes a test runner and other functionality that will make things easier for test development. To install the plugin, in Eclipse, go to Help --> Software Updates --> Find and Install... From there, select "Search for new feature to install" and click "Next>". In the upper right hand corner, click on "New Remote Site". Enter "TestNG Plugin" as the Name and http://beust.com/eclipse as the URL and click OK. Make sure TestNG is the only thing with a check next to it and click Finish. In the new window that is displayed, check the box next to testng and click Next>. On the following screen, accept the license agreement and click Next>. Click Finish. Once the download is complete, Eclipse will ask you what to install. Click Install All. Once the install is complete, restart Eclipse. Once Eclipse is back, right click on your project and click Properties. Select Java Build Path from the left and click on the Libraries tab. On the right, click Add Variable and select TESTNG_HOME. Click the Extend... button to the right and select /lib/testng-jdk15.jar and click Ok. Click OK to exit the Properties window.

We now need to get jmockit. This is the mock framework we will be using for this tutorial. To download jmockit, go to here and download the JMockit 0.94c release. Once the download is complete, extract the jmockit.jar file from the archive and place it in the lib folder we created previously. To add the new jar to your classpath, right click on your project and select Properties. Go to Java Build Path and select the Libraries tab. From there, click on Add JARs.... Select the jmockit jar we just copied into the lib directory and click Ok. Click Ok to exit the properties window. You are now ready to code!

The scenario

In test driven design, we develop the unit test before the functionality. We write a test that verifies that the method should do X after our call. We prove that the test fails, we then create the component to make the test pass. In this case, we are going to create a service with a method that authenticates a user. Below is a class diagram of the scenario.

TestNG class diagram

The interfaces

As stated before, we will be testing the same scenario as we did in the previous tutorial. To review, we will start our coding by defining two interfaces, LoginService and UserDAO We will implement LoginService, however since in this tutorial UserDAO will be mocked, we won't bother implementing it right now. For LoginService, we have a single method that takes a String userName and String password and returns a boolean (true if the user was found, false if it was not). The interface looks like this:

* Provides authenticated related processing.
public interface LoginService {

     * Handles a request to login.  Passwords are stored as an MD5 Hash in
     * this system.  The login service creates a hash based on the paramters
     * received and looks up the user.  If a user with the same userName and
     * password hash are found, true is returned, else false is returned.
     * @parameter userName
     * @parameter password
     * @return    boolean
     boolean login(String userName, String password);

The UserDAO interface will look very similar to the LoginService. It will have a single method that takes a userName and hash. The hash is an MD5 hashed version of the password, provided by the above service.

* Provides database access for login related functions
public interface UserDAO {

     * Loads a User object for the record that
     * is returned with the same userName and password.
     * @parameter userName
     * @parameter password
     * @return    User
     User loadByUsernameAndPassword(String userName, String password);


Before we begin development, we will develop our test. Tests are structured by grouping methods that perform a test together in a test case. TestNG subscribes well to POJO development styles by not requiring you extend any class or implement any interface. The test lifecycle is directed via annotations (we will be covering standard JDK 1.5+ annotations in this tutorial, TestNG does support JDK 1.4 as well. It requires an additional JAR file and uses XDoclet style annotations). The annotations we will cover in this tutorial are:

  • @Before*/@After*
    The before and after series of annotations tell the test runner to execute the annotated method either before or after the specified test, group, method, suite or class.
  • @Test
    The @Test annotation indicates to the runner that a method is a test method.

The method we will be testing is the login method on the LoginService. Because this method has a dependency on the UserDAO, we will have to create a mock for it. In this tutorial we will be using the jmockit mocking framework. Before we start writing any test code, let's cover some important points about the jmockit framework.


Most mocking frameworks (JMock, EasyMock, etc) are based on the java.lang.Proxy object. They create a proxy object based on the interface you provide to be injected into the object to be tested. The developer tells the proxy what to expect and what to return when the expectation is met. There are two issues with this:

  1. It requires an interface. Don't get me wrong, programming to interfaces is a good thing, but not everything requires an interface. EasyMock does have an extension that handles classes without an interface, but it can be a pain to have two different EasyMock objects included in your test class to do basically the same thing.
  2. Dependency injection of some kind is required. Don't get me wrong, I love Spring and use it on my current project. But what if you don't want to have Spring manage everything. You cannot mock an object that is created in your method. The only way to use mock objects is if you control the instantiation of an object elsewhere (in a factory typically).

jmockit addresses these two issues using the redefinition ability. If you are unfamiliar with the new ability to redefine classes in JDK 1.5+, essentially what it does is it allows you to programatically tell the JVM to remap a class to a different class file. So when you create a mock with jmockit, you will be creating a new class that the JVM will replace the dependent class with.

The test case

So now that we know a tiny bit about both TestNG and jmockit, let's start to flush out our test case. To start, well create an empty TestNG testcase with the appropriate imports:

import org.testng.annotations.*;

public class LoginServiceTest {


Note that the class does not extend any class or implement any interface. This is because TestNG does not require it. We will dictate all of the lifecycle stages using annotations. As we noted before, the login method is dependent on the UserDAO. Because of this, let's create a setupMocks method to be run before each test. The setupMocks method will create any required mocks we need to create and inject them accordingly.

import mockit.Expectations;

import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class LoginServiceTest extends Expectations {
	private LoginServiceImpl service;
	UserDAO mockDao;
	public void setupMocks() {
		service = new LoginServiceImpl();
		service.setUserDao( mockDao );

Let's go over what we have in the above class. We have the two required imports for this test case, mockit.Mockit (the main utility for jmockit) and org.testng.annotations.* to import the required TestNG annotations we will use. We have declared the two objects we care about: the service to be tested and the mock DAO to be injected. Note, LoginService is private but UserDAO is not. jmockit will create a mock object for the mockDao automatically for any non private fields.

Next we have our setupMocks method. This has TestNG's @BeforeMethod annotation. This tells TestNG to run this method once before each test. This will give us a fresh instance to test so that we don't have to be concerned with any clean-up that may be required. We then create the instance of our service to be tested and finally, inject the mock object into our implementation. If this looks alot like EasyMock, it should. The usage of jmockit and EasyMock are very similar when using interfaces. It's when you use classes that the usage differs.

The next step is to create our actual test method. In our test method, we want to test the following scenario:

TestNG sequence diagram

Even with the very basic method we want to test above, there are still a number of different scenarios that require tests. We will start with the "rosy" scenario, passing in two values and getting a user object back. Below is the updated test with our new test method.

	 * This method will test the "rosy" scenario of passing a valid 
         * username and password and retrieveing the user. Once the user 
         * is returned to the service, the service will return true to 
         * the caller.
	public void testRosyScenario() {
		User results = new User();
		String userName = "testUserName";
		String password = "testPassword";
		String passwordHash = 

                     mockDao.loadByUsernameAndPassword( userName, 
                                                        passwordHash ), 
                     results );

		assert service.login( userName, password ) : 
                       "Expected true, but was false";

So let's go thru the code above. We start out with our annotation @Test. This tells TestNG that this is a test method. Once in the method, we create the expected result of our DAO call, results. In this case, our method will just check to see if an object was returned, so we don't need to populate our user object with anything, we just need an empty instance. Next we declare the values we will be passing into our service call. The password hash may catch you off guard. It's considered unsafe to store passwords as plain text so our service will generate an MD5 hash of the password and that value is the value that we will pass to our DAO.

Once declaring the various variables, the next call:

     mockDao.loadByUsernameAndPassword( userName, passwordHash ), 
     results );

tells jmockit to expect the method loadByUsernameAndPassword to be called with the values previously declared in userName and passwordHash. When that happens, return the value results. The next line:


tells jmockit that we are done recording the expectations for this test case. jmockit automatically goes into replay mode at this point. Finally we perform the actual "test" by executing the method to be tested, login and asserting that it is true. If it is not, we present a message. TestNG considers a test passing if no exceptions are thrown. If an exception is thrown from a method that is declared a test, the test fails.

To run our test case, we have one more thing to configure. We need to tell the JVM that we will have a java agent running. To do that, right click on the LoginServiceTest, select Run As --> Open Run Dialog. On the left, select TestNG. At the top, click on the new icon. In the new area to the right, Enter LoginServiceTest in the Class box. Click the Arguments tab at the top. In the VM Arguments box, enter the following (where <PATH_TO_LIB> is the fully qualified path to the lib folder of your project):


All that is left is to click Run. What? The test failed?! That's because we haven't implemented the method we are testing yet. The code for that is below.

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class LoginServiceImpl implements LoginService {
	UserDAO userDao;

	public void setUserDao(UserDAO userDao) {
		this.userDao = userDao;
	public boolean login(String userName, String password) {
        boolean valid = false;
        try {
             String passwordHash = null;
             MessageDigest md5 = MessageDigest.getInstance("MD5");
             passwordHash = new String(md5.digest());

             User results = 
                  userDao.loadByUsernameAndPassword(userName, passwordHash);
             if(results != null) {
                  valid = true;
        } catch (NoSuchAlgorithmException ignore) {}
        return valid;


As you can see, TestNG provides a robust featureset that isn't foreign to someone who is used to JUnit styles of unit tests. In part two of this tutorial, we will refactor the LoginService to take better advantage of the jmockit framework and create additional tests that we can organize with TestNG. Until next time!

31 Responses

  • sudhir responded:

    its good

  • shashi responded:


    anybody to help,hwo we can use jmockit for classes in different packages,i.e when the class to be mocked uses methods of different class of different module


  • dan responded:

    won't work for me, some information is missing in this tutorial. invokeReturning() and endRecording() are undefined. Sample code like in your previous tutorial would be nice to track down what i am missing.


  • rajkumar responded:

    hi, it was good

  • MattS responded:

    This is a great tutorial. I just wanted to mention that if you download and use the latest version of jmockit it won't work. You need to use the version which the author mentions. There are a couple methods which have been deprecated (i.e. invokeReturning). Also the structure of the test case has changed and you no longer extend your test case with Expectations. Otherwise, a great example!


  • Greg S. responded:

    I would appreciate if the source was listed as a download like some of your other tutorials. I am trying to learn how to use unit testing and mocking and when i have issues in a tutorial, it really helps if I have reference code to look through to see what mistake I am making.

  • Fake Hublot Watches for sale responded:

    Close to wrist watches, the fashion home of Tag Heuerdefinitely understands what they are undertaking.

  • replica patek philippe responded:

    Numerous luxurious watches for males are developed in a few incredibly costly metals which contains platinum, gold or chrome steel.

  • Ganesh responded:

    A very good tutorial to start with TestNG and JMockit.

  • Varun responded:

    Thanks for providing good article. http://reditblog.blogspot.in

  • williama responded:

    A few time of exercise with rc choppers will make sure that you can take off, area, float, and do various antenna techniques without failing your valuable chopper into the floor or into any opponent. Google

  • ddanielgrant responded:

    You can select from a variety of choppers but should make sure that you select an cost-effective and durable design that is simple to fly and sustain in the long run. https://www.google.com.pk/

  • salman responded:

    This makes it difficult for webmasters to do it themselves as they neither have adequate time nor experience and knowledge to handle or understand the projects related. The best option for them is to outsource the SEO and subsequently weblink developing procedure. https://www.fiverr.com/johnsmithrese/get-you-reddit-traffic-buy-reddit-upvotes

  • stock photo free responded:

    Thus, in dealing with a topic like SEO, large competitors results in even the multi-redundant hyperbole recommended above or it's version.

  • curvy bust cream responded:

    Mockit it shows in error in the editior there isnt any Mockit call or annotation available it seems.

  • venkat responded:

    Why do we need to set javaagent???

  • Reddy responded:

    Have you written a post on using TestNG with Mockito?

  • tile shops melbourne responded:

    excellent post... i love it....

  • Hostgator Cloud Web Hosting Review responded:

    The article you have shared here is very awesome. I really like and appreciate your work. The points you have mentioned in this article are useful .

  • vijay responded:

    This page is more informative about the Unit Testing with TestNG and beginners can get the know about the selenium web driver. People willing to get the indepth knowledge in getting framework such as TestNG,jenkins,Maven and POM should get properly get best selenium training in chennai

  • free tour stockholm responded:

    All the blog works are dazzling. The more I read, the more fall under the spell of them.

  • z3 car covers responded:

    The blog has a to a great degree limit center that amplifies, shockingly into extensive variety of concerns.

  • Facebook advertising responded:

    The most outstanding component about this blog is the means by which it doesn't neglect to keep its gathering of people connected with at each point.

  • kitchen design responded:

    Been hunting down such a wonderful blog like this for quite a while.

  • http://www.zerobones.com/make-your-skin-glowing-with-the-help-of-the-melanotan-tanning-creams/ responded:

    This blog gives a strong affirmation of outstanding composed work.

  • collegeessayservice.my-free.website responded:

    The author has so delightfully enraptured the consideration of crowd by this radiant blog.

  • loemrntdhe responded:

    your post is very useful, I am truly happy to post my note on this blog.

  • bola88 responded:

    We need additional such articles that we are able to browse with such energy.

  • Putnam responded:

    A very stylistic way of writing, creating effects especially in language and literature.

  • Tallahassee Car Title Loans responded:

    The most outstanding component about this blog is the means by which it doesn't neglect to keep its gathering of people connected with at each point.

  • holm law group san diego responded:

    In this age of hypocrisy, the writer has written every word with all his soul poured into it.

Add your own comment...