easymock接口模拟_EasyMock好又严格的模拟

easymock接口模拟

EasyMock mock objects can have three types – default, strict and nice. We can specify mock object type using MockType enum while creating the mock object.

EasyMock模拟对象可以具有三种类型-默认,严格和良好。 我们可以在创建模拟对象时使用MockType 枚举指定模拟对象类型。

EasyMock模拟类型 (EasyMock Mock Types)

  1. Default: Expects only recorded calls but in any order. Test won’t fail if any recorded calls are skipped.

    默认值:仅预期录制的呼叫,但以任何顺序。 如果跳过任何录制的呼叫,测试不会失败。
  2. Strict: Expects only recorded calls and in the same order they have been recorded.

    严格:仅预期已录制的呼叫,并且录制的顺序相同。
  3. Nice: Expects recorded calls in any order and return default empty values (0, null, false) for unexpected calls.

    尼斯:预期以任何顺序记录的呼叫,并为意外呼叫返回默认的空值(0,null,false)。

We can create strict or nice mock object by specifying it’s mock type.

我们可以通过指定模拟类型来创建严格或不错的模拟对象。

Object mock = mock(MockType.STRICT, Object.class);

Object mock1 = mock(MockType.NICE, Object.class);

If you are using @Mock annotation, then you can specify the mock type by any of following ways.

如果使用@Mock注解 ,则可以通过以下任何一种方式指定模拟类型。

@Mock(MockType.STRICT)
Object obj;

@Mock(type=MockType.NICE)
Object obj1;

EasyMock严格的模拟示例 (EasyMock Strict Mock Example)

Let’s look at a simple example of the strict mock type. We will record few behaviors of an ArrayList mock object and replay them in the same order.

让我们看一个严格的模拟类型的简单示例。 我们将记录ArrayList模拟对象的一些行为,并以相同顺序重播它们。

package com.journaldev.easymock;

import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.mock;
import static org.easymock.EasyMock.replay;
import static org.easymock.EasyMock.verify;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.ArrayList;

import org.easymock.MockType;
import org.junit.jupiter.api.Test;

public class EasyMockStrictMockExample {

	@Test
	public void test() {
		ArrayList<Integer> mockList = mock(MockType.STRICT, ArrayList.class);
		expect(mockList.add(10)).andReturn(true);
		expect(mockList.add(20)).andReturn(true);
		expect(mockList.size()).andReturn(2);
		expect(mockList.get(0)).andReturn(10);
		replay(mockList);

		mockList.add(10);
		mockList.add(20);
		assertEquals(mockList.size(), 2);
		assertTrue(mockList.get(0) == 10);

		verify(mockList);
	}
}

If an unexpected method is called on a strict mock object, the error message will show the method calls expected at this point followed by the first conflicting method call.

如果在严格的模拟对象上调用了意外的方法,则错误消息将显示此时期望的方法调用,然后是第一个冲突的方法调用。

If there are missing calls, then verify() method error message will show all missing method calls.

如果缺少调用,则verify()方法错误消息将显示所有缺少的方法调用。

EasyMock Nice Mock示例 (EasyMock Nice Mock Example)

Now let’s have a look at the nice mock example. We will make some unrecorded calls and notice that the returned value is the default value of the method return type.

现在,让我们看一下漂亮的模拟示例。 我们将进行一些未记录的调用,并注意返回的值是方法返回类型的默认值。

package com.journaldev.easymock;

import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.mock;
import static org.easymock.EasyMock.replay;
import static org.easymock.EasyMock.verify;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.assertFalse;

import java.util.ArrayList;

import org.easymock.Mock;
import org.easymock.MockType;
import org.junit.jupiter.api.Test;

public class EasyMockNiceMockExample {
	
	@Test
	public void test() {
		ArrayList<Integer> mockList = mock(MockType.NICE, ArrayList.class);
		expect(mockList.add(10)).andReturn(true);
		expect(mockList.size()).andReturn(2);
		expect(mockList.get(0)).andReturn(10);
		replay(mockList);

		mockList.add(10);
		// below will NOT throw exception because of nice mock
		boolean b = mockList.add(30);
		assertFalse(b);

		assertEquals(mockList.size(), 2);

		assertTrue(mockList.get(0) == 10);
		//verify won't throw error for unexpected calls for nice mock
		verify(mockList);
	}
}

摘要 (Summary)

EasyMock strict mocking can be handy when our test methods have to be called in specific order. For example, CRUD operations mocking. Similarly, if we want to allow unexpected calls to pass, we can use nice mock.

当必须按特定顺序调用我们的测试方法时,EasyMock严格的模拟可以派上用场。 例如,CRUD操作模拟。 同样,如果我们希望允许意外调用通过,则可以使用漂亮的模拟。

GitHub Repository. GitHub存储库中检出完整的项目和更多EasyMock示例。

翻译自: https://www.journaldev.com/22234/easymock-nice-strict-mock

easymock接口模拟

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值