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)));
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));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With