如何断言groupType!= null包含4个分支 [英] How does assert groupType != null contain 4 branches
本文介绍了如何断言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);
}
在我的测试案例中,我同时传递了应覆盖两个分支的null和非null值,但是代码覆盖率表明缺少4个分支中的1个,另外2个分支又是什么? 以下是我的测试用例.
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);
}
推荐答案
EclEmma Eclipse插件使用 JaCoCo提供覆盖率分析库,然后依次分析字节码.让我们举一个简化的例子:
EclEmma Eclipse plugin provides coverage analysis using JaCoCo library, which in his turn analyzes bytecode. Let's take a simplified example:
class Example {
void example(Object x) {
assert x != null;
}
}
让我们看一下它的字节码:
And let's have a look on its bytecode:
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
如您所见-实际上有两个条件:
As you can see - there are actually 2 conditions:
- 已启用断言(
ifne
) -
x
不是null
(ifnonnull
)
- assertions enabled (
ifne
) x
is notnull
(ifnonnull
)
还有4种可能的变体.
这篇关于如何断言groupType!= null包含4个分支的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文