Mockito equivalent to this Hamcrest “samePropertyValuesAs”/jMock “with” idiom?

こ雲淡風輕ζ 提交于 2019-12-11 05:48:41

问题


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

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 comes from import static org.hamcrest.beans.SamePropertyValuesAs.samePropertyValuesAs;
with appears to come from jMock

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

... 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.


回答1:


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

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


来源:https://stackoverflow.com/questions/39930835/mockito-equivalent-to-this-hamcrest-samepropertyvaluesas-jmock-with-idiom

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!