모키토와 함께 "방법을 해결할 수 없습니다"
사용합니다.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그리고.thenReturn()방법은 다음과 같습니다.
OngoingStubbing<T> thenReturn(T value);
무엇이 문제입니까?
다른 구문을 사용하여 와일드카드 일치 일반과 함께 컬렉션을 반환해 보십시오.doReturn(grantedAuthorities).when(authentication).getAuthorities();
이것.doReturn호출은 유형 안전하지 않으며 유형에 대한 런타임 검사를 수행하지만 사용자의 목적에 따라 사용자가 원하는 모의 목록을 반환합니다.
모키토를 사용한 디테일과 와일드카드를 사용한 제네릭이 많습니다.자세한 내용: http://www.angelikalanger.com/GenericsFAQ/FAQSections/TypeArguments.html#Wildcards
유형 안전을 보장하려면 다음과 같이 컬렉션을 초기화할 수 있습니다.
Collection grantedAuthorities =
Lists.newArrayList(
new SimpleGrantedAuthority(AuthoritiesConstants.USER));
그럼 평소에 사용하시면 됩니다.when(authentication.getAuthorities()).thenReturn(grantedAuthorities);
이 솔루션의 장점은grantedAuthorities의 유형은 보존되어 있습니다.
자세한 내용은 이 기사를 참조하십시오.
언급URL : https://stackoverflow.com/questions/51168430/cannot-resolve-method-with-mockito
'programing' 카테고리의 다른 글
| 텍스트가 swift UI로 감싸지지 않습니다. (0) | 2023.09.06 |
|---|---|
| 테두리 길이가 div 너비보다 작습니까? (0) | 2023.09.06 |
| SD 카드에 디렉터리를 자동으로 만드는 방법 (0) | 2023.09.01 |
| jQuery 다중 ID 선택기 (0) | 2023.09.01 |
| 모든 비트가 0이면 IEEE 부동 소수점의 값은 얼마입니까? (0) | 2023.09.01 |