2016-05-24 21 views
0

メソッドのためにJUnit APIを使用してテストケースを作成しています。私はすべてのシナリオをカバーしましたが、私に苦労しているのはifブロックです。私はこの行にカーソルを合わせると、Coberturaは各条件に対して50%50%と述べていますが、これをどのようにカバーするのか正確にはわかりません。テスト中のJUnitとMockitoによるブランチカバレッジ

方法:

protected boolean isDateWithinTimelineRange(Calendar date, ServiceContext ctx) { 
    Calendar end = (Calendar)ctx.getParameter(ServiceConstants.TIMELINE_END); 
    Calendar start = (Calendar)ctx.getParameter(ServiceConstants.TIMELINE_BEGIN); 

    if(end != null && start != null) { 
     if(date.getTimeInMillis() >= start.getTimeInMillis() && date.getTimeInMillis() <= end.getTimeInMillis()) { 
      return true; 
     } else { 
      return false; 
     } 
    } 

    return true; 
} 

JUnitテストケース:

@Test 
public void testIsDateWithinTimelineRange() throws Exception { 
    ServiceContext context = Mockito.mock(ServiceContext.class); 
    Calendar calender = Mockito.mock(Calendar.class); 

    Mockito.when(context.getParameter(Mockito.anyString())).thenReturn(calender); 

    TestBaseTimelineProvider provider = new TestBaseTimelineProvider(); 
    boolean answer = provider.isDateWithinTimelineRange(calender, context); 

    assertNotNull(answer); 
    assertTrue(provider.isDateWithinTimelineRange(calender, context)); 

    // Testing for NULL condition 
    context = Mockito.mock(ServiceContext.class); 
    calender = Mockito.mock(Calendar.class); 
    Mockito.when(context.getParameter(Mockito.anyString())).thenReturn(null); 

    provider = new TestBaseTimelineProvider(); 
    answer = provider.isDateWithinTimelineRange(calender, context); 

    assertNotNull(answer); 
    assertTrue(provider.isDateWithinTimelineRange(calender, context)); 

    // Start date set to null 
    context = Mockito.mock(ServiceContext.class); 
    calender = Mockito.mock(Calendar.class); 
    ServiceConstants constants = new ServiceConstants(); 

    Mockito.when(context.getParameter(ServiceConstants.TIMELINE_END)).thenReturn(calender); 

    provider = new TestBaseTimelineProvider(); 
    answer = provider.isDateWithinTimelineRange(calender, context); 

    assertNotNull(constants); 

    // End date set to null 
    context = Mockito.mock(ServiceContext.class); 
    calender = Mockito.mock(Calendar.class); 
    constants = new ServiceConstants(); 

    Mockito.when(context.getParameter(ServiceConstants.TIMELINE_BEGIN)).thenReturn(calender); 

    provider = new TestBaseTimelineProvider(); 
    answer = provider.isDateWithinTimelineRange(calender, context); 

    assertNotNull(constants); 
} 

私を混乱させる何が、私はモックとendstart変数の値を決定していたパラメータdateです。

if(date.getTimeInMillis() >= start.getTimeInMillis() && date.getTimeInMillis() <= end.getTimeInMillis()) {}は、私がカバーしたいラインです。

おかげ

+1

'getTimeInMillis()'メソッドで返すものを模擬する必要はありません。たとえば、 'Mockito.when(calender.getTimeInMillis())。then return(1L); ? 'date.getTimeInMillis()'を呼び出すと、 'date'オブジェクトが偽装されたために何も返されませんでしたが、' getTimeInMillis() 'メソッドのために何も返さないように設定されました。 – Draken

+0

偽(最初の項目が真ではない、または最初の項目が真ではなく、2番目の項目が真ではない)を返す場合は、2つのケースをカバーしませんでした。その条件に対して有効なテストでassertFalse(provider.isDateWithinTimelineRange)が少なくとも1つ、おそらく2つのケースを持つ必要があります。 – user1676075

+0

ええ、実際に私はそれを介してデバッグしていたので、それらの2つの同様のテストケースを得ました。私はgetTimeMillis()に取り組んでいます。 –

答えて

1

はまず、あなたの嘲笑カレンダーがgetTimeInMillis()が呼び出されたときに何をすべきかのオブジェクト語ったことはありません。あなたは、各カレンダーエントリの次の項目を追加する必要があります。

// Assume `long desiredlong` defined; 
Mockito.when(calendar.getTimeInMillis()).thenReturn(desiredlong); 

あなたはdate.getTimeInMillis()が所望の範囲内にあり、別のセットdate.getTimeInMillisどこのカレンダーオブジェクトのセットのためにこれを行う必要があります()が希望の範囲外です。

最終的には、そのテストの本当の側面を覆うケースは次の形式を取る:

@Test 
public void validDatesInRange() { 
    ServiceContext context = Mockito.mock(ServiceContext.class); 
    Calendar calenderstart = Mockito.mock(Calendar.class); 
    Mockito.when(calendarstart.getTimeInMillis()).thenReturn(1L); 

    Calendar calendertarget = Mockito.mock(Calendar.class); 
    Mockito.when(calendartarget.getTimeInMillis()).thenReturn(2L); 

    Calendar calenderend = Mockito.mock(Calendar.class); 
    Mockito.when(calendarend.getTimeInMillis()).thenReturn(3L); 

    Mockito.when(context.getParameter(ServiceConstants.TIMELINE_END)).thenReturn(calenderend); 
    Mockito.when(context.getParameter(ServiceConstants.TIMELINE_BEGIN)).thenReturn(calenderstart); 

    TestBaseTimelineProvider provider = new TestBaseTimelineProvider(); 
    boolean answer = provider.isDateWithinTimelineRange(calendertarget, context); 

    assertNotNull(answer); 
    assertTrue(provider.isDateWithinTimelineRange(calendartarget, context)); 
} 

は第二に、あなたが実際に偽のリターンをテスト何を書いたことはありません。反対側をカバーするには上記をコピーしますが、には1000Lのような何かを返すように設定し、アサーションをfalseに変更してください。

また、名前contextIsNull()endDateIsNull()startDateIsNull()validDatesNotInRange()validDatesInRange()のような、何のための個々のテストメソッドのチェックを反映する複数の方法にあなたのテストケースを破損することもできます。そうすることで、テストはより小さく理解しやすくなり、デバッグも簡単になります。テストを実行すると、よりクリーンで有益なテストレポートが作成され、1つのテストに失敗しても他のテストの失敗は隠されません。

+0

詳しい投稿はBrandonにありがとうございます。私は間違いなくあなたが推薦するものに従います。 –

関連する問題