1
我需要測試下面的代碼。assert groupType!= null如何包含4個分支
public List<PatientGroup> findGroups(final String groupType) throws HwBaseAppException
{
assert groupType != null;//4 branches here
CriteriaBuilder criteriaBuilder=persistence.getCriteriaBuilder();
CriteriaQuery<PatientGroup> query = criteriaBuilder.createQuery(PatientGroup.class);
Root<PatientGroup> patientGroupRoot = query.from(PatientGroup.class);
Predicate condition=criteriaBuilder.equal(patientGroupRoot.get(PatientGroup_.active), Boolean.TRUE);
Join<PatientGroup, GroupSubType> groupSubTypeRoot = patientGroupRoot.join(PatientGroup_.groupSubType);
Predicate condition1=criteriaBuilder.equal(groupSubTypeRoot.get(GroupSubType_.groupTypeCode), groupType);
query.where(condition,condition1);
query.orderBy(criteriaBuilder.asc(patientGroupRoot.get(PatientGroup_.name)));
TypedQuery<PatientGroup> tq = persistence.createQuery(query);
List<PatientGroup> collections = tq.getResultList();
return initializeGroupRelationships(collections);
}
在我的測試案例我傳遞這應該涵蓋樹枝然而代碼報道稱1的4個分支missed.What是其他2個分支兩空,而不是空值? 以下是我的測試用例。
@Test
public void testFindGroups_1()
throws HwBaseAppException
{
Mockito.when(persistence.getCriteriaBuilder()).thenReturn(criteriaBuilder);
Mockito.when(criteriaBuilder.createQuery(PatientGroup.class)).thenReturn(criteriaQuery);
Mockito.when(criteriaQuery.from(PatientGroup.class)).thenReturn(patientGroupRoot);
Mockito.when(patientGroupRoot.join(PatientGroup_.groupSubType)).thenReturn(groupSubTypeRoot);
Mockito.when(persistence.createQuery(Mockito.any(CriteriaQuery.class))).thenReturn(tq);
Mockito.when(tq.getResultList()).thenReturn(arrayList);
Mockito.when(arrayList.iterator()).thenReturn(iterator);
Mockito.when(iterator.hasNext()).thenReturn(true).thenReturn(false);
Mockito.when(iterator.next()).thenReturn(patientGroup);
groupServiceDAOImpl.findGroups("groupType");
}
@Test(expected=AssertionError.class)
public void testFindGroups_2()
throws HwBaseAppException
{
groupServiceDAOImpl.findGroups(null);
}
請問您可以請我幫助我如何獲得(斷言啓用(ifne))左側分支覆蓋,根據您的輸入我已經通過0作爲參數,但它似乎沒有幫助。 –
@AviralSaxena參見http://docs.oracle.com/javase/7/docs/technotes/guides/language/assert.html#enable-disable關於如何在Java中啓用/禁用斷言。談到測試和代碼質量 - 在這種情況下可能是沒有必要覆蓋所有分支機構。 – Godin
@AviralSaxena btw不要忘了upvote並接受一個答案,如果它回答你的問題。謝謝。 – Godin