相当于该Hamcrest"samePropertyValuesAs"/jMock"with"的Mockito.成语? [英] Mockito equivalent to this Hamcrest "samePropertyValuesAs"/jMock "with" idiom?

查看:161
本文介绍了相当于该Hamcrest"samePropertyValuesAs"/jMock"with"的Mockito.成语?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hamcrest/jMock代码如下:

Hamcrest/jMock code looks like this:

@Test
public void setsSniperValuesInColumns() {
    context.checking(new Expectations() {{
        one(listener).tableChanged(with(aRowChangedEvent())); 
    }});
    model.sniperStatusChanged(new SniperState("item id", 555, 666), MainWindow.STATUS_BIDDING);
    ...
}

private Matcher<TableModelEvent> aRowChangedEvent() {
    return samePropertyValuesAs(new TableModelEvent(model, 0));
}

请注意,这取自测试指导下的面向对象软件的增长"(第157页).本书的作者使用Hamcrest和jMock.我认为AssertJ和Mockito可能更好.当然,可以在同一个项目中同时使用这两个测试框架,但这会造成很大的混乱,而且似乎并不理想.

NB this is taken from "Growing Object-Oriented Software Guided by Tests" (p. 157). The authors of this book use Hamcrest and jMock. I'm of the opinion that AssertJ and Mockito are probably better. Of course it would be possible to use both these testing frameworks in the same projects, but it would get pretty confusing and doesn't seem ideal.

samePropertyValuesAs来自import static org.hamcrest.beans.SamePropertyValuesAs.samePropertyValuesAs;
with似乎来自jMock

samePropertyValuesAs comes from import static org.hamcrest.beans.SamePropertyValuesAs.samePropertyValuesAs;
with appears to come from jMock

所以我想找到的是一种在他们使用Expectations的地方可以使用Mockito的verify方法的方式.但是我有什么办法可以做到这一点:

So what I'm trying to find is a way that I can use Mockito's verify method where they are using Expectations. But is there any way that I can do this:

verify( listener ).tableChanged( samePropertyValues( new TableModelEvent( model, 0 )));

...当然,可以想象出一种变通方法,即您可以一一设置所有属性...但是我想Mockito具有更好的功能.

... of course one can imagine a workaround where you go around setting all the properties one by one... but I would imagine Mockito has something better out of the box.

推荐答案

The refEq matcher seems like it's what you're looking for:

verify(listener).tableChanged(refEq(new TableModelEvent(model, 0)));

这篇关于相当于该Hamcrest"samePropertyValuesAs"/jMock"with"的Mockito.成语?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆