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