Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何验证使用带有特定字段的参数调用了mock';价值观_Java_Unit Testing_Mocking_Mockito - Fatal编程技术网

Java 如何验证使用带有特定字段的参数调用了mock';价值观

Java 如何验证使用带有特定字段的参数调用了mock';价值观,java,unit-testing,mocking,mockito,Java,Unit Testing,Mocking,Mockito,我用mockitomock写了一个UT 我想验证是否使用以下参数调用了mock:MyObject obj其中obj={name=…,value=9} 我想验证是否使用值为9的param调用了mock 你会怎么做 我不想在myObject中重写equals(..),mockito有ArgumentMatcher接口: class IsListOfTwoElements extends ArgumentMatcher<List> { public boolean matche

我用mockitomock写了一个UT

我想验证是否使用以下参数调用了mock:
MyObject obj
其中obj=
{name=…,value=9}

我想验证是否使用值为9的param调用了mock

你会怎么做


我不想在
myObject
中重写
equals(..)
,mockito有ArgumentMatcher接口:

 class IsListOfTwoElements extends ArgumentMatcher<List> {
     public boolean matches(Object list) {
         return ((List) list).size() == 2;
     }
 }

 List mock = mock(List.class);

 when(mock.addAll(argThat(new IsListOfTwoElements()))).thenReturn(true);
类IsListOfTwoElements扩展ArgumentMatcher{
公共布尔匹配(对象列表){
返回((列表)列表).size()=2;
}
}
List mock=mock(List.class);
when(mock.addAll(argThat(new IsListOfTwoElements()))。然后返回(true);

这是一个内置的匹配器:
hasProperty

assertThat(externalPois, Matchers.hasItem(Matchers.<ExternalPoi>hasProperty("id", is(5))));
assertThat(externalPois,Matchers.hasItem(Matchers.hasProperty(“id”,is(5)));