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)
- Default: Expects only recorded calls but in any order. Test won’t fail if any recorded calls are skipped. 默认值:仅预期录制的呼叫,但以任何顺序。 如果跳过任何录制的呼叫,测试不会失败。
- Strict: Expects only recorded calls and in the same order they have been recorded. 严格:仅预期已录制的呼叫,并且录制的顺序相同。
- 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操作模拟。 同样,如果我们希望允许意外调用通过,则可以使用漂亮的模拟。
翻译自: https://www.journaldev.com/22234/easymock-nice-strict-mock
easymock接口模拟