I need to test the following code.
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); }
In my test case i am passing both null and not null values which should cover both the branches however Code coverage says 1 of 4 branches missed.What are the other 2 branches? Below are my test cases.
@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); }