使用mockito“无法解析方法”

2024-01-09

I use 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>)'

我用的是莫基托2.15.0 and thenReturn()其方法是:

OngoingStubbing<T> thenReturn(T value);

问题是什么?


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

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

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用mockito“无法解析方法” 的相关文章

随机推荐