assert groupType != null 怎么包含4个分支
How does assert groupType != null contain 4 branches
我需要测试以下代码。
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);
}
在我的测试用例中,我同时传递了空值和非空值,它们应该覆盖两个分支但是代码覆盖率说 4 个分支中的 1 个 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);
}
EclEmma Eclipse 插件使用 JaCoCo library 提供覆盖率分析,后者又分析字节码。让我们举一个简单的例子:
class Example {
void example(Object x) {
assert x != null;
}
}
让我们看看它的字节码:
javac Example.java
javap -v Example
看起来像:
0: getstatic #2 // Field $assertionsDisabled:Z
3: ifne 18
6: aload_1
7: ifnonnull 18
10: new #3 // class java/lang/AssertionError
13: dup
14: invokespecial #4 // Method java/lang/AssertionError."<init>":()V
17: athrow
18: return
如您所见 - 实际上有 2 个条件:
- 断言已启用(
ifne
)
x
不是 null
(ifnonnull
)
还有 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);
}
在我的测试用例中,我同时传递了空值和非空值,它们应该覆盖两个分支但是代码覆盖率说 4 个分支中的 1 个 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);
}
EclEmma Eclipse 插件使用 JaCoCo library 提供覆盖率分析,后者又分析字节码。让我们举一个简单的例子:
class Example {
void example(Object x) {
assert x != null;
}
}
让我们看看它的字节码:
javac Example.java
javap -v Example
看起来像:
0: getstatic #2 // Field $assertionsDisabled:Z
3: ifne 18
6: aload_1
7: ifnonnull 18
10: new #3 // class java/lang/AssertionError
13: dup
14: invokespecial #4 // Method java/lang/AssertionError."<init>":()V
17: athrow
18: return
如您所见 - 实际上有 2 个条件:
- 断言已启用(
ifne
) x
不是null
(ifnonnull
)
还有 4 种可能的变体。