我该如何使用org.mockito.AdditionalMatchers.gt?

时间:2013-02-22 09:26:24

标签: mockito assert hamcrest

我正在试图找出org.mockito.AdditionalMatchers如何运作,但我失败了。为什么这个测试失败了?

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>

1 个答案:

答案 0 :(得分:9)

在这种情况下你应该使用Hamcrest的greaterThangt用于验证模拟对象中方法调用的参数:

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

}