Search code examples
javapropertiesmockitohamcrestjmock

Mockito equivalent to this Hamcrest "samePropertyValuesAs"/jMock "with" idiom?


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.


Solution

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

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