Hamcrest matcher for Object ...参数

时间:2011-11-24 17:03:52

标签: java jmock hamcrest

我有类

的方法
interface Class1{
    void method1(SomeObject... parameters);
}

我有一个自定义的Hamcrest匹配器

public class SomeObjectMatcher extends BaseMatcher<SomeObject>{...}

如何编写期望匹配传递给method1的对象

SomeObject someObject = new SomeObject(...);
...

mockery.checking(new Expectations(){{
    oneOf(class1).method1(with(new SomeObjectMatcher(someObject1)));
}}

实际通话

class1.method1(someObject);

传递的对象和期望的对象是相同的,但SomeObjectMatcher失败,因为实际传递的参数不是someObject1,而是SomeObject [] {someObject1}(只有一个对象的数组 - someObject1)? 有没有办法在链中添加新的匹配器,如

oneOf(class1).method1(with(arrayHas(new SomeObjectMatcher(someObject1))));

1 个答案:

答案 0 :(得分:1)

尝试将arrayHas替换为hasItemInArray。要匹配数组中的多个项目,您可以使用arrayContainingarrayContainingInAnyOrder