gpt4 book ai didi

java - 空指针异常取决于 Mockito 中的语句链接?

转载 作者:行者123 更新时间:2023-11-30 03:26:36 26 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;
}
}

运行良好。但如果我修改这些行如下:

final Criteria criteria = session.createCriteria(SelectionVariant.class).add(Restrictions.eq("client", selectionVariant.getClient()));
criteria.add(Restrictions.eq("variant.variantName", selectionVariant.getVariant().getVariantName()));

我会得到:

java.lang.NullPointerException
at com.innflow.ebtam.dao.hibernate.variant.SelectionVariantUniqueConstraintValidatorImpl.buildValidationCriteria(SelectionVariantUniqueConstraintValidatorImpl.java:35)
为什么?我缺少什么?

这是我的测试类:

@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", Mockito.any(Client.class)));
}
}

最佳答案

您必须模拟Criteriaadd 方法。这就是这里发生的事情。在您的第一个版本中,您模拟了标准实例。当您调用方法 add() 时,它不会执行任何操作并返回 null 但它不会打扰您,因为您不使用返回值。

但是,您在第二个版本中确实使用了 add() 的返回值。为了使其正常工作,您应该:

  1. 使mockito调用真实的add()方法\
  2. 模拟 add() 方法以使其返回原始条件

例如:

when(criteria.add(any(Restrictions.class))).thenCallRealMethod();

when(criteria.add(any(Restrictions.class))).thenReturn(criteria)

(我不确定它应该是Restrictions.class:它应该是Restrictions.eq()的类)

关于java - 空指针异常取决于 Mockito 中的语句链接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30047559/

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