2013-02-22 24 views
6

Próbuję dowiedzieć się, jak działa org.mockito.AdditionalMatchers, ale mi się nie udało. Dlaczego ten test kończy się niepowodzeniem?Jak korzystać z org.mockito.AdditionalMatchers.gt?

import static org.hamcrest.CoreMatchers.is; 
import static org.junit.Assert.*; 
import static org.mockito.AdditionalMatchers.*; 

public class DemoTest { 

    @Test 
    public void testGreaterThan() throws Exception { 

     assertThat(17 
      , is(gt(10)) 
     ); 
    } 
} 

wyjściowa wynosi:

java.lang.AssertionError: 
Expected: is <0> 
    got: <17> 

Odpowiedz

6

Należy użyć Hamcrest na greaterThan dla tej sprawy. gt służy do sprawdzania argumentów wywołań metod w próbnych obiektach:

public class DemoTest { 

    private List<Integer> list = Mockito.mock(List.class); 

    @Test 
    public void testGreaterThan() throws Exception { 
     assertThat(17, is(org.hamcrest.Matchers.greaterThan(10))); 

     list.add(17); 
     verify(list).add(org.mockito.AdditionalMatchers.gt(10)); 
    } 

}