TestNG Mockito示例

时间:2020-02-23 14:41:57  来源:igfitidea点击:

在Mockito教程中,我们使用JUnit运行测试用例。
Mockito框架也可以与其他Java测试框架结合。
在本教程中,我们将把Mockito模拟框架与TestNG测试框架集成在一起。

TestNG Mockito

我们将重用Mockito教程中创建的类,并在TestNG中为其编写测试用例。
您可以从我们的GitHub存储库下载完整的代码,以检查这些类。
为简单起见,我们有以下程序。

  • AddService接口和AddServiceImpl实现类。

  • CalcService是依赖于AddService的服务类。

我们的目标是测试CalcService类方法,因此我们将使用Mockito而不是创建其实例来模拟AddService。

TestNG Mockito模拟()示例

让我们看一下TestNG测试类,其中我们将使用Mockito.mock()方法来模拟AddService

package com.theitroad.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.theitroad.AddService;
import com.theitroad.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 }, };
	}
}

这是从Eclipse执行上述TestNG测试类时的输出。

如果要通过maven命令行执行TestNG测试类,则将以下依赖项添加到maven-surefire-plugin中。

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

您可以从我们的GitHub项目存储库中检出完整的pom.xml文件代码。

TestNG Mockito @Mock注释示例

我们还可以使用@Mock批注将模拟对象注入TestNG测试,只需确保在@ BeforeMethod方法中调用MockitoAnnotations.initMocks(this),以便Mockito将初始化模拟对象。

package com.theitroad.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.theitroad.AddService;
import com.theitroad.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模拟混凝土程序

我们可以模拟一个具体的类,并为特定操作指定行为。
让我们模拟ArrayList并对其一些操作进行存根。

package com.theitroad.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示例

package com.theitroad.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()示例

package com.theitroad.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();

	}
}