我使用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
Intellij 给出了以下编译错误:
Cannot resolve method 'thenReturn(java.util.Collection<org.spring.security.core.authority.SimpleGrantedAuthority>)'
我使用 Mockito 2.15.0
和 thenReturn()
方法:
OngoingStubbing<T> thenReturn(T value);
问题是什么?
最佳答案
尝试使用其他语法返回带有通配符匹配泛型的集合:
doReturn(grantedAuthorities).when(authentication).getAuthorities();
此 doReturn
调用不是类型安全的,会导致运行时检查类型,但出于您的目的,它将返回您想要的模拟列表。
有很多使用mockito和带有通配符的泛型的细节。更多细节: http://www.angelikalanger.com/GenericsFAQ/FAQSections/TypeArguments.html#Wildcards
关于spring - "Cannot resolve method"与mockito,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51168430/