gpt4 book ai didi

java - 使用FunctionalInterface/MethodReference参数对调用进行 stub /验证

转载 作者:行者123 更新时间:2023-12-02 13:00:37 26 4
gpt4 key购买 nike

我从来没有必要对接受函数接口(interface)/方法引用作为参数的调用进行 stub ,所以除了引用比较之外,我只是发现明显无法与“等于”这些进行比较。

那么在测试过程中如何处理这种情况呢?无论我们想要与否,我们都必须使用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

最佳答案

由于 GOOS 书中有这样的通信协议(protocol)测试,因此需要在一个简单的测试用例中验证其协作者是否收到了精确的Comparator。描述它们是否能够正确地协同工作。然后使用 any(Comparator.class) 跳过检查其余测试。您可以在Martin's blog中进一步阅读它:

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.

关于java - 使用FunctionalInterface/MethodReference参数对调用进行 stub /验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44313875/

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