【问题标题】:Hamcrest matcher for Object... parameters用于 Object... 参数的 Hamcrest 匹配器
【发布时间】:2011-11-24 17:03:52
【问题描述】:

我得到了类的方法

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

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

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

如何编写与传递给方法1的对象匹配的期望

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))));

【问题讨论】:

    标签: java jmock hamcrest


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-07
      • 1970-01-01
      • 1970-01-01
      • 2015-09-15
      • 1970-01-01
      相关资源
      最近更新 更多