TestNG Mockito Example

Filed Under: Mockito

In the Mockito Tutorial, we used JUnit to run test cases. Mockito framework can be combined with other Java testing frameworks too. In this tutorial, we will integrate Mockito mocking framework with TestNG testing framework.

TestNG Mockito

We will reuse the classes created in the Mockito Tutorial and write test cases for them in TestNG. You can download the complete code from our GitHub Repository to check out these classes. To give a brief idea, we have following classes.

  • AddService interface and AddServiceImpl implementation class.
  • CalcService is the service class that has dependency on AddService.

Our goal is to test CalcService class methods, so we will mock AddService using Mockito rather than creating its instance.

TestNG Mockito mock() example

Let’s look at the TestNG test class where we will mock AddService using Mockito.mock() method.


package com.journaldev.mockito.testng;

import static org.mockito.Mockito.when;
import static org.testng.Assert.assertEquals;

import org.mockito.Mockito;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import com.journaldev.AddService;
import com.journaldev.CalcService;

public class TestNGMockitoExample {

	@Test(dataProvider = "dp")
	public void test_mock_object(int i, int j) {
		System.out.println("**--- Test testCalc executed ---**");

		AddService addService;
		CalcService calcService;

		addService = Mockito.mock(AddService.class);
		calcService = new CalcService(addService);

		int expected = i + j;
		when(addService.add(i, j)).thenReturn(expected);

		int actual = calcService.calc(i, j);

		assertEquals(expected, actual);
	}

	@DataProvider
	public Object[][] dp() {
		return new Object[][] { new Object[] { 1, 1 }, new Object[] { 2, 2 }, };
	}
}

Here is the output when above TestNG test class is executed from Eclipse.

TestNG Mockito Example

If you want to execute TestNG test classes through maven command line, then add following dependencies to maven-surefire-plugin.


<dependency>
	<groupId>org.apache.maven.surefire</groupId>
	<artifactId>surefire-testng</artifactId>
	<version>2.22.0</version>
</dependency>

You can check out the complete pom.xml file code from our GitHub project repository.

TestNG Mockito @Mock Annotation Example

We can also use @Mock annotation to inject mocked objects to TestNG tests, just make sure to call MockitoAnnotations.initMocks(this) in the @BeforeMethod method, so that Mockito will initialize the mocked objects.


package com.journaldev.mockito.testng;

import static org.mockito.Mockito.when;
import static org.testng.Assert.assertEquals;

import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import com.journaldev.AddService;
import com.journaldev.CalcService;

public class TestNGMockAnnotationExample {

	CalcService calcService;

	@Mock
	private AddService addService;

	@BeforeMethod
	public void setup() {
		System.out.println("@BeforeMethod TestNGMockAnnotationExample");
		MockitoAnnotations.initMocks(this);
	}

	@Test(dataProvider = "dp")
	public void test_mock_annotation(int i, int j) {
		System.out.println("**--- Test testCalc executed ---**");

		calcService = new CalcService(addService);

		int expected = i + j;
		when(addService.add(i, j)).thenReturn(expected);

		int actual = calcService.calc(i, j);

		assertEquals(expected, actual);

	}

	@DataProvider
	public Object[][] dp() {
		return new Object[][] { new Object[] { 1, 1 }, new Object[] { 2, 2 }, };
	}
}

TestNG Mock Concrete Class

We can mock a concrete class and specify the behavior for a specific operation. Let’s mock ArrayList and stub few of its operations.


package com.journaldev.mockito.testng;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.testng.Assert.assertEquals;

import java.util.ArrayList;

import org.testng.annotations.Test;

public class TestNGMockitoSingleElement {

	@SuppressWarnings("unchecked")
	@Test
	public void mock_concrete_class() {
		ArrayList<String> mockedList = mock(ArrayList.class);

		when(mockedList.get(0)).thenReturn("first-element");

		System.out.println(mockedList.get(0));
		assertEquals("first-element", mockedList.get(0));

		// "null" gets printed as get(1) is not stubbed
		System.out.println(mockedList.get(1));
	}
}

TestNG Mockito Spy Example


package com.journaldev.mockito.testng;

import static org.testng.Assert.assertEquals;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;

import java.util.ArrayList;
import java.util.List;

import org.testng.annotations.Test;

public class TestNGMockitoSpyExample {

	@Test
	public void test_mockito_spy() {
		List<String> list = new ArrayList<>();
		List<String> listSpy = spy(list);

		listSpy.add("first-element");
		System.out.println(listSpy.get(0));

		assertEquals("first-element", listSpy.get(0));
		when(listSpy.get(0)).thenReturn("second-element");

		System.out.println(listSpy.get(0));
		assertEquals("second-element", listSpy.get(0));

		// call the real method on Spied object since it's not stubbed
		assertEquals(1, listSpy.size());

	}
}

TestNG Mockito verify() example


package com.journaldev.mockito.testng;

import static org.mockito.Mockito.verify;

import java.util.List;

import org.mockito.Mockito;
import org.mockito.internal.verification.VerificationModeFactory;
import org.testng.annotations.Test;

public class TestNGMockitoVerify {
	@Test
	public void test_mockito_verify() {

		@SuppressWarnings("unchecked")
		List<String> mockedList = Mockito.mock(List.class);

		mockedList.add("first-element");
		mockedList.add("second-element");
		mockedList.add("third-element");
		mockedList.add("third-element");
		mockedList.clear();

		verify(mockedList).add("first-element");
		verify(mockedList).add("second-element");
		verify(mockedList, VerificationModeFactory.times(2)).add("third-element");

		verify(mockedList).clear();

	}
}

Summary

Mockito is a fantastic mocking framework and it’s easily integrated with TestNG testing framework. We looked at various TestNG Mockito examples and both frameworks use fluent coding style.

You can download the complete project code from our GitHub Repository.

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages