我必须编写一个方法,它能够在数据库中找到随机假期。但是当我在测试中使用这部分
Random id = new Random();
vacationId = id.nextInt(2500);
我明白了NullPoiterExeption
。但是当我只是打印
vacationId = 2500;
或其他数字,一切正常。
这是整个方法
public Vacation findRandomVacation() {
Integer vacationId = null;
Query query = entityManager.createQuery("from Vacation v where v.id = :id")
.setParameter("id", vacationId);
if ( query.getResultList().isEmpty()) {
Random id = new Random();
vacationId = id.nextInt(2500);
query = entityManager.createQuery("from Vacation v where v.id = :id")
.setParameter("id", vacationId);
}
return (Vacation) query.getResultList().get(0);
}
这是测试的主体:
@Test
public void testVacationApprovalResultDao () {
Vacation testVacation = findRandomVacation();
VacationApproval testVacationApproval = findVacationApprovalDAO(testVacation);
List<VacationApprovalResult> testVacationApprovalResult = getVacationApprovalResultByManager(testVacationApproval);
Set<VacationApprovalResult> setVacationApprovalResult = testVacationApproval.getVacationApprovalResults(); //mistake is here
List<VacationApprovalResult> testVacationApprovalResult2 = new ArrayList<VacationApprovalResult> ();
testVacationApprovalResult2.addAll(setVacationApprovalResult);
assertEquals(testVacationApprovalResult, testVacationApprovalResult2);
}