gpt4 book ai didi

java - 如何使用 Mockito 比较 Junit 测试用例中的多个条件

转载 作者:行者123 更新时间:2023-12-01 11:45:45 25 4
gpt4 key购买 nike

我想使用 Mockito 在 Junit 测试用例中给出多个条件。我需要使用mockito的Junit测试用例的代码如下。帮助我解决这个问题。

      Customer customer;//Cutomer is a class;
String temp;
if(customer.isSetValid() &&
StringUtil.hasvalue(temp=customer.isGetValid.getValue()))

如何在 Mockito 中使用多个条件。语法 is-When(conditions).thenReturn(true);

最佳答案

when 条件是方法的输入参数,而不是 if 条件,因此您可以传递两个方法参数,这些参数将成为模拟的条件。

因此,在模拟方法时,您可以传递一个模拟的客户和一个 temp 值,您将在测试该方法时将其传递给该方法,这样模拟将返回您在 thenReturn 函数中传递的任何内容。

您还可以使用匹配器,例如 any

关于java - 如何使用 Mockito 比较 Junit 测试用例中的多个条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29148301/

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