gpt4 book ai didi

java - Mockito 说参数不相同,但它们是?

转载 作者:搜寻专家 更新时间:2023-10-31 20:01:37 25 4
gpt4 key购买 nike

这是我要测试的方法:

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

这是我的测试:

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

这是我在控制台中看到的:

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)

我错过了什么?

编辑:

Client.java中的equals方法如下:

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

编辑:

Client.java 中的 hashCode 方法:

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

编辑:

这使得测试工作:

Mockito.verify(criteria,Mockito.atLeastOnce()).add(Restrictions.eq("client", Mockito.any(Client.class)));

最佳答案

criteria.add 的参数不是Client,而是Restrictions.eq 的返回值, 一个 SimpleExpression .

SimpleExpressionObject 继承其 .equals-method,因此由于您在测试中构造了一个新的,因此两个参数不相等,即使它们包含相同的 Client

按照您想要的方式比较参数的一种方法是像这样实现 org.hamcrest.Matcher:

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

然后像这样验证:

 Matcher<SimpleExpression> matcher = new SimpleExpressionMatcher(Restrictions.eq("client", selectionVariant.getClient()));
Mockito.verify(criteria).add(Mockito.argThat(matcher));

关于java - Mockito 说参数不相同,但它们是?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30046751/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com