2016-12-13 25 views
-1

私はこのようなJava 8の述語を持っています。どのように私はあなたが(それはあなたの例ではと思われるような)述語への参照を持っているなら、私は何の問題を見ていないこのユニットテストJava 8述語

Predicate<DTO> isDone = (dtO) -> 
       (!dto.isFinished() && 
       !dto.isCompleted()); 

おかげ

+1

http://stackoverflow.com/questions/28688047/unit-test-code-with-java-8-lambdas – YoungSpice

+1

の重複**テストではさまざまなことについて言葉で**を書いてください。あなたはあなたの説明に特定の入力と予想される出力を含めるべきです。 –

答えて

1

のためのユニットテストを書くのですか。ここではMockito/JUnitを用いた簡単な例です:

@Mock 
private DTO mockDTO; 

@Test 
public void testIsDone_Finished_NotComplete() 
{ 
    when(mockDTO.isFinished()).thenReturn(true); 
    when(mockDTO.isCompleted()).thenReturn(false); 

    boolean expected = false; 
    boolean actual = isDone.test(mockDTO); 

    Assert.assertEquals(expected, actual); 
} 
1

私はそのようにそれをテストします:

private final Predicate<DTO> isDone = (dto) -> 
     (!dto.isFinished() && !dto.isCompleted()); 

@Test 
public void a() throws Exception { 
    // given 
    DTO dto = new DTO(true, true); 

    // when 
    boolean result = isDone.test(dto); 

    // then 
    assertThat(result).isFalse(); 
} 

@Test 
public void s() throws Exception { 
    // given 
    DTO dto = new DTO(true, false); 

    // when 
    boolean result = isDone.test(dto); 

    // then 
    assertThat(result).isFalse(); 
} 

@Test 
public void d() throws Exception { 
    // given 
    DTO dto = new DTO(false, true); 

    // when 
    boolean result = isDone.test(dto); 

    // then 
    assertThat(result).isFalse(); 
} 

@Test 
public void f() throws Exception { 
    // given 
    DTO dto = new DTO(false, false); 

    // when 
    boolean result = isDone.test(dto); 

    // then 
    assertThat(result).isTrue(); 
} 

@AllArgsConstructor 
public static class DTO { 
    private final boolean isFinished; 
    private final boolean isCompleted; 

    public boolean isFinished() { 
     return isFinished; 
    } 

    public boolean isCompleted() { 
     return isCompleted; 
    } 
} 

の代わりに、S、およびF名を適切のようなテスト:should_be_done_when_it_is_both_finished_and_completed。

私はDTOが単なる値オブジェクトだと思うので、私はむしろ模擬を使用する代わりに実際のインスタンスを作成したいと思います。