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))));
I got method of class
interface Class1{
void method1(SomeObject... parameters);
}
I have a custom Hamcrest matcher
public class SomeObjectMatcher extends BaseMatcher<SomeObject>{...}
How to write expectation matching that object passed to the method1
SomeObject someObject = new SomeObject(...);
...
mockery.checking(new Expectations(){{
oneOf(class1).method1(with(new SomeObjectMatcher(someObject1)));
}}
The actual call is
class1.method1(someObject);
The passed object and the expected one are same, but SomeObjectMatcher fails, because the actual passed parameter is not someObject1, but it is SomeObject[]{someObject1} (array with only one object - someObject1)?
Is there a way to add a new matcher in the chain, something like
oneOf(class1).method1(with(arrayHas(new SomeObjectMatcher(someObject1))));
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试用
hasItemInArray
替换arrayHas
。要匹配数组中的多个项目,您可以使用 arrayContaining 和 arrayContainingInAnyOrder。Try replacing
arrayHas
withhasItemInArray
. To match multiple items in an array you can usearrayContaining
andarrayContainingInAnyOrder
.