“无法解析方法"与嘲笑 [英] "Cannot resolve method" with mockito

查看:145
本文介绍了“无法解析方法"与嘲笑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用具有方法的org.springframework.security.core.Authentication:

Collection<? extends GrantedAuthority> getAuthorities();

我想如下模拟它:

when(authentication.getAuthorities()).thenReturn(grantedAuthorities);

具有权限收集:

Collection<SimpleGrantedAuthority> grantedAuthorities = Lists.newArrayList(
        new SimpleGrantedAuthority(AuthoritiesConstants.USER));

我正在使用org.springframework.security.core.authority.SimpleGrantedAuthority,它扩展了GrantedAuthority

And I am using org.springframework.security.core.authority.SimpleGrantedAuthority which extends GrantedAuthority

然后Intellij给我下面的编译错误:

And Intellij gives me below compile error:

Cannot resolve method 'thenReturn(java.util.Collection<org.spring.security.core.authority.SimpleGrantedAuthority>)'

我使用Mockito 2.15.0thenReturn()方法是:

I use Mockito 2.15.0 and thenReturn() method from it is:

OngoingStubbing<T> thenReturn(T value);

出什么问题了?

推荐答案

尝试使用其他语法来返回带有与通配符匹配的泛型的集合: doReturn(grantedAuthorities).when(authentication).getAuthorities();

Try using the other syntax to return your collection with a wildcard matching generic: doReturn(grantedAuthorities).when(authentication).getAuthorities();

doReturn调用不是类型安全的,会导致对类型的运行时检查,但出于您的目的,它将返回您想要的模拟列表.

This doReturn call isn't type-safe and results in a runtime check on type but for your purposes it will return the mocked list you want.

使用通配符的模仿和泛型有很多细节.更多细节: http://www.angelikalanger.com/GenericsFAQ/FAQSections/TypeArguments.html#Wildcards

There are a lot of details using mockito and generics with wildcards. For more details: http://www.angelikalanger.com/GenericsFAQ/FAQSections/TypeArguments.html#Wildcards

这篇关于“无法解析方法"与嘲笑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆