Mockito equivalent to this Hamcrest “samePropertyV

2019-07-25 03:08发布

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条回答
Rolldiameter
2楼-- · 2019-07-25 04:06

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

verify(listener).tableChanged(refEq(new TableModelEvent(model, 0)));
查看更多
登录 后发表回答