Stubing/Verifying 带有 FunctionalInterface/MethodReference 参数的调用

Stubing/Verifying a call with a FunctionalInterface/MethodReference argument

我从来不需要存根接受功能性 interface/method 引用作为参数的调用,所以我只是发现除了引用比较之外,与 "equals" 进行比较的明显无能为力。

那么在测试过程中如何处理这种情况呢?我们是否必须使用 any() 是否需要?

Comparator<String> comparator = Comparator.naturalOrder();

when(myColaborator.isCalledWith(comparator)).thenReturn("foo"); //This is not gonna work as long as the real call does not use theat very same reference

有必要在一个简单的测试用例中验证它的合作者是否收到了一个准确的Comparator,因为这样一个通讯协议测试在一本书中[=15] =]:

in the second test case is that I've relaxed the constraints on the expectation by using withAnyArguments. The reason for this is that the first test checks that the number is passed to the warehouse, so the second test need not repeat that element of the test. If the logic of the order needs to be changed later, then only one test will fail, easing the effort of migrating the tests. As it turns out I could have left withAnyArguments out entirely, as that is the default.