Using Mockito with TestNG

Previous
Next

TestNG is an open source automated testing framework which is meant to perform unit, functional, end-to-end, integration testing. TestNG need minimum Java JDK 5. Based on the website,

TestNG is a testing framework inspired from JUnit and NUnit but introducing some new functionalities that make it more powerful and easier to use like Annotation, Support for parameters etc.

In TestNG, NG means Next Generation. TestNG is similar to JUnit although, it is not a JUnit extension. It is surely motivated from JUnit. Integration testing can be done much better in TestNG in comparison to JUnit.

JUnit vs TestNG

TestNG and JUnit differ at their core behavior:

  • JUnit instantiates the class during all of its test methods. This means that these fields are unique or local to each test case we write and they are not shared across test cases, which can be a downside too.
  • In TestNG, just one object is created whose state of fields are shared across @Test  cases we write.

To enable the use of Mockito annotations in your Test cases like @Mock or @Spy or many other annotations, we must programmatically enable them. This can be done by doing the following:

For JUnit, it works out of the box because 2nd @Test has its own fields and its own mocks.

Mockito with TestNG

Now, let us look at how stubbing in TestNG works, we’ll use Mockito to mock as well.

Prerequisites

I am assuming that you have installed TestNG plugin in eclipse.

Steps for creating Mockito TestNG example.
Step 1: Create a simple java maven project.

Maven Dependency

Step 2: Add required dependencies to pom.xml

A Maven dependency is the fastest way to get started with Mockito:

Just a single dependency which doesn’t bring any other libraries with it. See here for latest versions of the library.

As we will also be using some TestNG functionality, we will also need its dependency. Let’s add it next,

Your pom.xml will look as below:

Step 3: Let’s define our class to test:

Finally, let’s put above simple class to test.

Step 4: Create testing.xml

Step 5: Run the test
Right click on testng.xml and select “TestNG Suite” and you will get below output:
TestNG
Be aware that TestNG does not recreate your test class for multiple test methods in the same test class (and, unfortunately, Mockito won’t recreate the @InjecktMocks objects if the instances are not null), which will lead to your mocks / tested objects not being recreated for different test methods, even though the initMocks call is in a @beforemethod-annotated method.

Why use TestNG?

Without @Mock annotation in Mockito, the verify() calls will be made for all @test annotated methods. This issue needs to be fixed, whose only solution is to use this gist or new up the mock in the @beforemethod (which didn’t seem reasonable). This default behavior is better, where the mock isn’t expected to persist state from previous tests.

Previous
Next

Join Our News Letter – Stay Updated

Subscribe to Awesome Java Content.




Add Comment

Join Our News Letter - Stay Updated

Subscribe to Awesome Java Content.
You can like our facebook page Java2blog