easymock参数_EasyMock捕获参数

easymock参数

Sometimes we want to stub behaviors for any input arguments, so we use argument matchers. If you want to know the arguments when these stub methods are being called, we have to use matcher that capture argument.

有时我们想对任何输入参数的行为进行存根,因此我们使用参数匹配器 。 如果要在调用这些存根方法时知道参数,则必须使用捕获参数的匹配器。

EasyMock捕获参数 (EasyMock Capture Arguments)

EasyMock capture arguments require following steps:

EasyMock捕获参数需要执行以下步骤:

  • Create Capture instance using EasyMock.newCapture() method.

    使用EasyMock.newCapture()方法创建Capture实例。
  • Use capture(Capture) argument matcher with expect to match any argument and also capture it for later use.

    使用带有expect匹配任何参数的capture(Capture)参数匹配器,并捕获它以备后用。
  • If you want to capture primitive types, there are specific methods such as captureInt(), captureBoolean() etc.

    如果要捕获原始类型,则有特定的方法,例如captureInt()captureBoolean()等。

单参数捕获 (Single Argument Capture)

If we capture a single argument, then we can get its value from Capture.getValue() method. Let’s have a quick example to capture single argument.

如果捕获单个参数,则可以从Capture.getValue()方法获取其值。 让我们举一个简单的例子来捕获单个参数。

package com.journaldev.easymock;

import static org.easymock.EasyMock.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.ArrayList;

import org.easymock.Capture;
import org.easymock.CaptureType;
import org.junit.jupiter.api.Test;

import com.journaldev.utils.MathUtils;

public class EasyMockCaptureAgrumentExample {

	@Test
	public void test_single_call_arg_capture() {
		ArrayList<String> mockList = mock(ArrayList.class);
		Capture
   
   
    
     captureSingleArgument = newCapture();
		expect(mockList.add(capture(captureSingleArgument))).andReturn(true);
		replay(mockList);

		assertTrue(mockList.add("Hello Pankaj"));
		
		System.out.println(captureSingleArgument.getValue()); //Hello Pankaj
		
		verify(mockList);
	}
}

   
   

捕获多个呼叫的参数 (Capture Arguments for Multiple Calls)

If the stubbed method is called multiple times, we can use getValues() to get the list of arguments.

如果多次调用存根方法,则可以使用getValues()获取参数列表。

@Test
public void test_multiple_calls_args_catcher() {
	ArrayList
   
   
    
     mockList = mock(ArrayList.class);
	Capture
    
    
     
      captureArguments = newCapture(CaptureType.ALL);
	expect(mockList.add(captureInt(captureArguments))).andReturn(true).times(2);
	replay(mockList);

	assertTrue(mockList.add(10));
	assertTrue(mockList.add(20));
	
	System.out.println(captureArguments.getValues()); //[10, 20]
	
	verify(mockList);
}

    
    
   
   

捕获多个参数 (Capture Multiple Arguments)

We can also capture multiple arguments methods. We can use different Capture instances for the different argument types or use the same instance.

我们还可以捕获多个参数方法。 我们可以将不同的Capture实例用于不同的参数类型,也可以使用相同的实例。

@Test
public void test_multiple_args_catcher() {
	MathUtils mock = mock(MathUtils.class);
	Capture<Integer> captureArguments = newCapture(CaptureType.ALL);

	expect(mock.add(captureInt(captureArguments), captureInt(captureArguments))).andReturn(10).times(2);
	replay(mock);

	assertEquals(mock.add(0,10), 10);
	assertEquals(mock.add(1, 9), 10);
	
	System.out.println(captureArguments.getValues()); //[0, 10, 1, 9]

	verify(mock);
}

Notice that I am using captureInt() in above example to capture integer arguments.

请注意,我在上面的示例中使用captureInt()来捕获整数参数。

摘要 (Summary)

EasyMock argument capture helps us in tracking and recording the arguments used with mocked methods. This can be helpful in debugging if our test cases are failing for any specific argument.

EasyMock参数捕获有助于我们跟踪和记录与模拟方法一起使用的参数。 如果我们的测试用例因任何特定参数而失败,这将有助于调试。

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

翻译自: https://www.journaldev.com/22296/easymock-capture-arguments

easymock参数

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值