当前位置: 首页 > 工具软件 > easyMock > 使用案例 >

easymock参数_EasyMock参数匹配器

易祖鹤
2023-12-01

easymock参数

EasyMock argument matchers allow us to provide the flexible argument for matching when stubbing the methods. You will find a lot of any*() methods in EasyMock that can be used with expect() to provide arguments for a method call.

EasyMock参数匹配器使我们可以在对方法进行存根时提供灵活的参数以进行匹配。 您将在EasyMock中找到许多any*()方法,这些方法可以与expect()一起使用,以提供方法调用的参数。

EasyMock参数匹配器示例 (EasyMock Argument Matchers Example)

Let’s look at a simple example of mocking ArrayList and stubbing its behaviors for any argument of a specific type.

让我们看一个模拟ArrayList并将其行为存入特定类型的任何参数的简单示例。

package com.journaldev.easymock;

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

import java.util.ArrayList;

import org.junit.jupiter.api.Test;

public class EasyMockAgrumentMatcherExample {

	@Test
	public void test() {
		ArrayList<Object> mockList = mock(ArrayList.class);
		expect(mockList.add(anyInt())).andReturn(true);
		expect(mockList.add(anyString())).andReturn(false);
		replay(mockList);

		assertTrue(mockList.add(10));
		assertFalse(mockList.add("Hi"));
		
		verify(mockList);
	}
}

Notice the use of anyInt() and anyString() for specifying the argument as any int or string object.

注意,使用anyInt()anyString()将参数指定为任何int或字符串对象。

Apart from matching any argument, there are some other argument matchers too. Let’s look at some of these argument matchers with example code.

除了匹配任何参数之外,还有其他一些参数匹配器。 让我们来看一些带有示例代码的参数匹配器。

平等论者匹配器 (Equality Argument Matcher)

We can use same() argument matcher method for stubbing behavior with same object. If you want to perform equality check, use eq() method.

我们可以使用same()参数匹配器方法对同一个对象的行为进行存根。 如果要执行相等性检查,请使用eq()方法。

package com.journaldev.easymock;

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

import java.util.ArrayList;

import org.junit.jupiter.api.Test;

public class EasyMockAgrumentMatcherEqualityExample {

	@Test
	public void test() {
		Utils mock = mock(Utils.class);
		Object obj = new Object();
		
		expect(mock.convert(same(obj))).andReturn("True");
		expect(mock.convert(eq("ABC"))).andReturn("Awesome");

		expect(mock.convert(anyObject())).andReturn("False");
		replay(mock);
		
		assertEquals("True", mock.convert(obj));
		assertEquals("Awesome", mock.convert("ABC"));

		assertEquals("False", mock.convert(new Object()));
	}
}

class Utils {
	public String convert(Object obj) {
		return obj.toString();
	}
}

比较参数匹配器 (Comparison Argument Matcher)

We can use lt(), gt(), leq() and geq() methods to compare arguments and return specific output. Note that these methods are overloaded to use with primitive data types as well as objects. If you are using them with any Object, then they should be Comparable.

我们可以使用lt()gt()leq()geq()方法来比较参数并返回特定的输出。 请注意,这些方法被重载以与原始数据类型以及对象一起使用。 如果将它们与任何对象一起使用,则它们应该是Comparable

package com.journaldev.easymock;

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

import java.util.ArrayList;
import java.util.List;

import org.junit.jupiter.api.Test;

public class EasyMockAgrumentMatcherComparisonExample {

	@Test
	public void test() {
		List
   
   
    
     mock = mock(ArrayList.class);
		
		expect(mock.add(lt(10))).andReturn(true);
		expect(mock.add(geq(10))).andReturn(false);
		replay(mock);
		
		assertTrue(mock.add(5));
		assertFalse(mock.add(100));

	}
}

   
   

条件参数匹配器 (Conditional Argument Matchers)

We can use and(), or() and not() functions with argument matchers to write more complex behaviors.

我们可以将and()or()not()函数与参数匹配器一起使用,以编写更复杂的行为。

Note that when we are using argument matchers to stub behaviors, every argument has to be a matcher otherwise, we will get an error message. So if we have to specify not(100), then write it as not(eq(100)).

请注意,当我们使用参数匹配器对行为进行存根时,每个参数都必须是匹配器,否则,我们将收到一条错误消息。 因此,如果我们必须指定not(100) ,则将其写为not(eq(100))

Here is a simple example of using conditional argument matchers.

这是使用条件参数匹配器的简单示例。

package com.journaldev.easymock;

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

import java.util.ArrayList;
import java.util.List;

import org.junit.jupiter.api.Test;

public class EasyMockAgrumentMatcherConditionalExample {

	@Test
	public void test() {
		List<Integer> mock = mock(ArrayList.class);
		
		//return true if number is between 0 to 10
		expect(mock.add(and(gt(0), lt(10)))).andReturn(true);
		
		//return true if number is 33 or 77
		expect(mock.add(or(eq(33), eq(77)))).andReturn(true);
		
		//return true if number is not 99
		expect(mock.add(not(lt(100)))).andReturn(false);		
				
		replay(mock);
		
		assertTrue(mock.add(5));
		assertTrue(mock.add(33));
		assertFalse(mock.add(102));
	}
}

空参数匹配器 (Null Argument Matchers)

We can use isNull() and notNull() to match null and not-null arguments. However, isNull() comes handy when we are using other argument matchers and can’t use null directly.

我们可以使用isNull()notNull()来匹配null和notNull()参数。 但是,当我们使用其他参数匹配器时, isNull()会派上用场,并且不能直接使用null。

List<Object> mock = mock(ArrayList.class);

expect(mock.add(isNull())).andReturn(true);
expect(mock.add(notNull())).andReturn(false);
replay(mock);

assertTrue(mock.add(null));
assertFalse(mock.add("Hi"));

字符串参数匹配器 (String Argument Matchers)

There are few utility methods especially for matching String arguments. These are:

实用程序方法很少,特别是用于匹配String参数的方法。 这些是:

  • startsWith(String)

    startsWith(String)
  • endsWith(String)

    EndsWith(String)
  • contains(String)

    contains(String)
  • matches(Regex): Expects argument string to match the regex.

    matchs(Regex):期望参数字符串与正则表达式匹配。
  • find(Regex): Expects one of the substring to match the regex.

    find(Regex):期望子字符串之一与正则表达式匹配。

Here is a simple example showing usage of string argument matchers.

这是一个简单的示例,显示了字符串参数匹配器的用法。

List<String> mock = mock(ArrayList.class);

expect(mock.add(startsWith("Java"))).andReturn(true);
expect(mock.add(endsWith("Dev"))).andReturn(true);
expect(mock.add(contains("Pyt"))).andReturn(true);
expect(mock.add(matches("^[abc]d."))).andReturn(true);
expect(mock.add(find("[9]{3}"))).andReturn(true);

replay(mock);

//startsWith Java
assertTrue(mock.add("Java World"));
//endsWith Dev
assertTrue(mock.add("JournalDev"));
//contains Pyt
assertTrue(mock.add("Python"));
//matches ads
assertTrue(mock.add("ads"));
// 999 is one of substring
assertTrue(mock.add("ABC999DDD")); 

verify(mock);

自定义参数匹配器 (Custom Argument Matcher)

Although EasyMock argument matchers support is extensive, if you want to implement a custom argument matcher then you can implement IArgumentMatcher interface and use it through EasyMock.reportMatcher(IArgumentMatcher).

尽管EasyMock参数匹配器支持广泛,但如果要实现自定义参数匹配器,则可以实现IArgumentMatcher接口,并通过EasyMock.reportMatcher(IArgumentMatcher)使用它。

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

翻译自: https://www.journaldev.com/22292/easymock-argument-matchers

easymock参数

 类似资料: