easymock_EasyMock验证

easymock

EasyMock Verify method is used to verify that all the stubbed methods are called and there are no unexpected calls on the mocked object.

EasyMock Verify方法用于验证是否调用了所有存根方法,并且在模拟对象上没有意外调用。

EasyMock验证 (EasyMock Verify)

EasyMock verify() method has the same effect as calling verifyRecording(Object) and verifyUnexpectedCalls(Object) methods.

EasyMock verify()方法与调用verifyRecording(Object)verifyUnexpectedCalls(Object)方法具有相同的效果。

Let’s look at a simple example of EasyMock verify() method.

让我们看一下EasyMock verify()方法的一个简单示例。

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.junit.jupiter.api.Test;

public class EasyMockVerifyExample {

	@Test
	public void test() {
		ArrayList<Integer> mockList = mock(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);
		assertTrue(mockList.get(0) == 10);
		assertEquals(mockList.size(), 2);

		verify(mockList);
	}
}

Notice that all the stubbed methods are being called after replay() method.

请注意,所有存根方法都在replay()方法之后被调用。

What if we don’t invoke a stubbed method?

如果我们不调用存根方法怎么办?

It will throw AssertionError. For example, if I comment mockList.add(10) statement then below exception stack trace will be reported by JUnit.

它将引发AssertionError。 例如,如果我注释了mockList.add(10)语句,则下面的异常堆栈跟踪将由JUnit报告。

java.lang.AssertionError: 
  Expectation failure on verify:
    ArrayList.add(10 (int)): expected: 1, actual: 0
	at org.easymock.internal.MocksControl.verify(MocksControl.java:284)
	at org.easymock.EasyMock.verify(EasyMock.java:2041)
	at com.journaldev.easymock.EasyMockVerifyExample.test(EasyMockVerifyExample.java:31)

EasyMock使用Nice Mock验证意外呼叫 (EasyMock verify unexpected calls with Nice Mock)

If the mocked object is a nice mock, then verify() method will not throw an error for unexpected method calls. However, it will throw an error if stubbed calls are not invoked.

如果模拟对象是一个不错的模拟对象,那么verify()方法将不会因意外的方法调用而引发错误。 但是,如果未调用存根调用,它将引发错误。

Below code snippet for test will pass with green colors because mocked object is nice mock.

下面的测试代码片段将以绿色传递,因为模拟对象是不错的模拟对象。

ArrayList
   
   
    
     mockList = mock(MockType.NICE, ArrayList.class);
replay(mockList);
mockList.add(10); //unexpected method call
verify(mockList);

   
   

EasyMock verify()复杂示例 (EasyMock verify() Complex Example)

We can move EasyMock verify() method to the @After methods to make sure all the stubbed methods were being called.

我们可以将EasyMock verify()方法移至@After方法,以确保调用了所有存根方法。

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.assertTrue;

import java.util.ArrayList;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class EasyMockVerifyRealExample {

	ArrayList<Integer> mockList;

	@BeforeEach
	public void setup() {
		mockList = mock(ArrayList.class);
	}

	@Test
	public void test() {
		expect(mockList.add(10)).andReturn(true);
		expect(mockList.get(0)).andReturn(10);
		replay(mockList);

		mockList.add(10);
		assertTrue(mockList.get(0) == 10);
	}

	@AfterEach
	public void teardown() {
		verify(mockList);
	}

}

摘要 (Summary)

EasyMock verify() method is used to make sure that all the stubbed methods are being utilized and there are no unexpected calls. The behavior for unexpected calls changes for nice mock objects where it doesn’t throw any error. EasyMock verify() method is very similar to Mockito verify() method.

EasyMock verify()方法用于确保使用了所有已存根方法,并且没有意外调用。 意外调用的行为会更改不会引发任何错误的精美模拟对象。 EasyMock verify()方法与Mockito verify()方法非常相似。

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

翻译自: https://www.journaldev.com/22228/easymock-verify

easymock

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值