2013-02-22 60 views
6

我想弄清楚org.mockito.AdditionalMatchers如何工作,但我失敗了。爲什麼這個測試失敗?我該如何使用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)) 
     ); 
    } 
} 

輸出是:

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

回答

6

您應該使用Hamcrest的greaterThan這種情況。 gt用於驗證模擬對象中方法調用的參數:

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)); 
    } 

}