Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mockito says arguments are not identical but they are?

This is the method that I want to test:

public class SelectionVariantUniqueConstraintValidatorImpl implements UniqueConstraintValidator {

    private SessionFactory sessionFactory;

    @Override
    public List<Criteria> buildValidationCriteria(Object entity, Serializable id, String[] propertyNames) {
        final SelectionVariant selectionVariant = (SelectionVariant) entity;
        final Session session = sessionFactory.getCurrentSession();
        final Criteria criteria = session.createCriteria(SelectionVariant.class);
        criteria.add(Restrictions.eq("client", selectionVariant.getClient()));
        criteria.add(Restrictions.eq("variant.variantName", selectionVariant.getVariant().getVariantName()));

        return new ArrayList<Criteria>(Arrays.asList(criteria));
    }

    @Required
    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }
}

And this is my test:

@RunWith(MockitoJUnitRunner.class)
public class SelectionVariantUniqueConstraintValidatorImplTest {

    SelectionVariantUniqueConstraintValidatorImpl selectionVariantUniqueConstraintValidator
            = new SelectionVariantUniqueConstraintValidatorImpl();

    @Mock
    private SessionFactory sessionFactory;
    @Mock
    private Session session;
    @Mock
    private Criteria criteria;

    @Before
    public void setUp() throws Exception {
        selectionVariantUniqueConstraintValidator.setSessionFactory(sessionFactory);
    }

    @Test
    public void testBuildValidationCriteria() throws Exception {
        Mockito.when(sessionFactory.getCurrentSession()).thenReturn(session);
        Mockito.when(session.createCriteria(SelectionVariant.class)).thenReturn(criteria);
        SelectionVariant selectionVariant = new SelectionVariant();
        Client client = new ClientBuilder().businessKey("HPD").description("desc").version(0).id(1L).build();
        selectionVariant.setClient(client);
        selectionVariantUniqueConstraintValidator.buildValidationCriteria(selectionVariant, null, null);
        Mockito.verify(criteria,Mockito.atLeastOnce()).add(Restrictions.eq("client", selectionVariant.getClient()));
    }
}

And this is what I see in the console:

Argument(s) are different! Wanted:
criteria.add(
    client=Client{id=1, businessKey='HPD', description='desc', version=0}
);
-> at com.innflow.ebtam.dao.hibernate.variant.SelectionVariantUniqueConstraintValidatorImplTest.testBuildValidationCriteria(SelectionVariantUniqueConstraintValidatorImplTest.java:48)
Actual invocation has different arguments:
criteria.add(
    client=Client{id=1, businessKey='HPD', description='desc', version=0}
);
-> at com.innflow.ebtam.dao.hibernate.variant.SelectionVariantUniqueConstraintValidatorImpl.buildValidationCriteria(SelectionVariantUniqueConstraintValidatorImpl.java:36)

What am I missing?

Edit:

equals method in Client.java is as follows:

@Override
public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || (getClass() != o.getClass() && !(o instanceof Client))) return false;

    Client client = (Client) o;

    if (businessKey != null ? !businessKey.equals(client.getBusinessKey()) : client.getBusinessKey() != null)
        return false;

    return true;
}

Edit:

hashCode method in Client.java :

@Override
public int hashCode() {
    return businessKey != null ? businessKey.hashCode() : 0;
}

Edit:

This makes the test work:

Mockito.verify(criteria,Mockito.atLeastOnce()).add(Restrictions.eq("client", Mockito.any(Client.class)));
like image 230
Koray Tugay Avatar asked May 05 '15 07:05

Koray Tugay


1 Answers

The argument to criteria.add is not a Client, but the return value of Restrictions.eq, a SimpleExpression.

SimpleExpression inherits its .equals-method from Object, so since you construct a new one in your test, the two arguments are not equal, even though they contain the same Client.

A way to compare the arguments the way you intend is to implement an org.hamcrest.Matcher like this:

class SimpleExpressionMatcher extends ArgumentMatcher<SimpleExpression> {
    private SimpleExpression simpleExpression;

    public SimpleExpressionMatcher(SimpleExpression simpleExpression) {
        this.simpleExpression = simpleExpression;
    }

    @Override
    public boolean matches(Object argument) {
        @SuppressWarnings({"unchecked", "rawtypes"})
        SimpleExpression otherSimpleExpression = (SimpleExpression) argument;
        // somehow compare the SimpleExpressions, possibly like this:
        // this.simpleExpression.toString().equals(otherSimpleExpression.toString());
    }
}

Then verify like this:

 Matcher<SimpleExpression> matcher = new SimpleExpressionMatcher(Restrictions.eq("client", selectionVariant.getClient()));
 Mockito.verify(criteria).add(Mockito.argThat(matcher));
like image 109
Bewusstsein Avatar answered Sep 28 '22 12:09

Bewusstsein