From bd18c05a278e2c03f920d86aaf98564cb57a8574 Mon Sep 17 00:00:00 2001 From: Marcus Da Coregio Date: Fri, 16 Sep 2022 15:54:31 -0300 Subject: [PATCH] Use mock class instead of interface on mock's return Issue gh-11860 --- .../annotation/web/OAuth2ResourceServerDslTests.kt | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/config/src/test/kotlin/org/springframework/security/config/annotation/web/OAuth2ResourceServerDslTests.kt b/config/src/test/kotlin/org/springframework/security/config/annotation/web/OAuth2ResourceServerDslTests.kt index cb75ba28da..c0631d162a 100644 --- a/config/src/test/kotlin/org/springframework/security/config/annotation/web/OAuth2ResourceServerDslTests.kt +++ b/config/src/test/kotlin/org/springframework/security/config/annotation/web/OAuth2ResourceServerDslTests.kt @@ -35,6 +35,7 @@ import org.springframework.security.config.annotation.web.builders.HttpSecurity import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity import org.springframework.security.config.test.SpringTestContext import org.springframework.security.config.test.SpringTestContextExtension +import org.springframework.security.core.Authentication import org.springframework.security.oauth2.core.oidc.IdTokenClaimNames.SUB import org.springframework.security.oauth2.jwt.Jwt import org.springframework.security.oauth2.jwt.JwtDecoder @@ -207,9 +208,7 @@ class OAuth2ResourceServerDslTests { mockkObject(AuthenticationManagerResolverConfig.RESOLVER) every { AuthenticationManagerResolverConfig.RESOLVER.resolve(any()) - } returns AuthenticationManager { - JwtAuthenticationToken(JWT) - } + } returns MockAuthenticationManager(JwtAuthenticationToken(JWT)) this.mockMvc.get("/") { header("Authorization", "Bearer token") @@ -241,6 +240,14 @@ class OAuth2ResourceServerDslTests { } } + class MockAuthenticationManager(var authentication: Authentication) : AuthenticationManager { + + override fun authenticate(authentication: Authentication?): Authentication { + return this.authentication + } + + } + @Test fun `oauth2Resource server when custom authentication manager resolver and opaque then exception`() { Assertions.assertThatExceptionOfType(BeanCreationException::class.java)