2013-02-22 34 views
6

Tôi đang cố gắng tìm ra cách hoạt động của org.mockito.AdditionalMatchers nhưng không thành công. Tại sao thử nghiệm này lại thất bại?Tôi nên sử dụng org.mockito.AdditionalMatchers.gt như thế nào?

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

Output là:

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

Trả lời

6

Bạn nên sử dụng hamcrest của greaterThan cho trường hợp này. gt là để xác minh đối số của các cuộc gọi phương thức trong các đối tượng giả:

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

} 
Các vấn đề liên quan