我试图弄清楚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>
对于这种情况,您应该使用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));
}
}