gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 13:51:34 27 4
gpt4 key购买 nike

我试图弄清楚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 的 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));
}

}

关于mockito - 我应该如何使用 org.mockito.AdditionalMatchers.gt?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15020747/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com