2016-12-29 12 views
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); 
    } 

回答

0

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個條件:

  1. assertions enabled(ifne
  2. x不是nullifnonnull

及SO 4可能的變型。

+0

請問您可以請我幫助我如何獲得(斷言啓用(ifne))左側分支覆蓋,根據您的輸入我已經通過0作爲參數,但它似乎沒有幫助。 –

+0

@AviralSaxena參見http://docs.oracle.com/javase/7/docs/technotes/guides/language/assert.html#enable-disable關於如何在Java中啓用/禁用斷言。談到測試和代碼質量 - 在這種情況下可能是沒有必要覆蓋所有分支機構。 – Godin

+0

@AviralSaxena btw不要忘了upvote並接受一個答案,如果它回答你的問題。謝謝。 – Godin

相關問題