Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 以方法参考为参数的单元测试法_Java_Unit Testing_Mockito - Fatal编程技术网

Java 以方法参考为参数的单元测试法

Java 以方法参考为参数的单元测试法,java,unit-testing,mockito,Java,Unit Testing,Mockito,我有一个将对象作为参数的方法和一个对该对象的方法引用,以从该对象收集集合中的整数。代码运行良好,但我无法在when()方法上使用mockito进行单元测试 这个很好用。我猜我需要通过一些其他的东西,然后将方法引用作为我第一次测试的参数,但是我很不幸地发现有其他人参与了这个案例,尽管这似乎很常见。对于mockito,您需要确保在语句时传递到的方法与实际方法传递的方法相同。根据您的示例,您正在使用UserCredentials::getAccountId和UserCredentials::accou

我有一个将对象作为参数的方法和一个对该对象的方法引用,以从该对象收集集合中的整数。代码运行良好,但我无法在when()方法上使用mockito进行单元测试


这个很好用。我猜我需要通过一些其他的东西,然后将方法引用作为我第一次测试的参数,但是我很不幸地发现有其他人参与了这个案例,尽管这似乎很常见。

对于mockito,您需要确保在语句时传递到
的方法与实际方法传递的方法相同。根据您的示例,您正在使用
UserCredentials::getAccountId
UserCredentials::accountId
(可能是打字错误)

您可能想考虑使用不同的参数匹配器:

import static org.mockito.ArgumentMatchers.*;

when(userCredentialsUtil.userCredentialsGetter(eq(userCredentials), any()).thenReturn(accountIds);

使用mockito时,您需要确保在
语句中传入的方法与实际方法传入的方法相同。根据您的示例,您正在使用
UserCredentials::getAccountId
UserCredentials::accountId
(可能是打字错误)

您可能想考虑使用不同的参数匹配器:

import static org.mockito.ArgumentMatchers.*;

when(userCredentialsUtil.userCredentialsGetter(eq(userCredentials), any()).thenReturn(accountIds);

是的,我打错了对不起。也许媒人会这么做。很高兴我能帮上忙!:)是的,我打错了对不起。也许媒人会这么做。很高兴我能帮上忙!:)
  public List<Integer> userCredentialsGetter(
      final List<UserCredentials> userCredentials) {
    return userCredentials.stream().map(UserCredentials::getAccountId).collect(Collectors.toList());
  }
        when(
        this.userCredentialsUtil.userCredentialsGetter(
            userCredentials)).thenReturn(accountIds);
import static org.mockito.ArgumentMatchers.*;

when(userCredentialsUtil.userCredentialsGetter(eq(userCredentials), any()).thenReturn(accountIds);