programing

모키토와 함께 "방법을 해결할 수 없습니다"

javajsp 2023. 9. 6. 21:47

모키토와 함께 "방법을 해결할 수 없습니다"

사용합니다.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