From 7dc4364f43a2c1467642c4a86c7e5113f5bc68f3 Mon Sep 17 00:00:00 2001 From: Eleftheria Stein Date: Tue, 26 Apr 2022 18:13:29 +0200 Subject: [PATCH] Fix Kotlin mockk test compatibility Issue gh-11039 --- .../annotation/web/SecurityContextDslTests.kt | 33 ++----------------- 1 file changed, 3 insertions(+), 30 deletions(-) diff --git a/config/src/test/kotlin/org/springframework/security/config/annotation/web/SecurityContextDslTests.kt b/config/src/test/kotlin/org/springframework/security/config/annotation/web/SecurityContextDslTests.kt index 2b9a5bed9c..e526fa237f 100644 --- a/config/src/test/kotlin/org/springframework/security/config/annotation/web/SecurityContextDslTests.kt +++ b/config/src/test/kotlin/org/springframework/security/config/annotation/web/SecurityContextDslTests.kt @@ -18,6 +18,7 @@ package org.springframework.security.config.annotation.web import io.mockk.every import io.mockk.mockk +import io.mockk.mockkObject import io.mockk.spyk import io.mockk.verify import org.assertj.core.api.Assertions.assertThat @@ -51,43 +52,15 @@ class SecurityContextDslTests { @Autowired lateinit var mvc: MockMvc - @Test - fun `configure when registering object post processor then invoked on security context persistence filter`() { - spring.register(ObjectPostProcessorConfig::class.java).autowire() - verify { ObjectPostProcessorConfig.objectPostProcessor.postProcess(any()) } - } - - @EnableWebSecurity - open class ObjectPostProcessorConfig : WebSecurityConfigurerAdapter() { - override fun configure(http: HttpSecurity) { - // @formatter:off - http { - securityContext { } - } - // @formatter:on - } - - @Bean - open fun objectPostProcessor(): ObjectPostProcessor = objectPostProcessor - - companion object { - var objectPostProcessor: ObjectPostProcessor = spyk(ReflectingObjectPostProcessor()) - - class ReflectingObjectPostProcessor : ObjectPostProcessor { - override fun postProcess(`object`: O): O = `object` - } - } - } - @Test fun `security context when invoked twice then uses original security context repository`() { spring.register(DuplicateDoesNotOverrideConfig::class.java).autowire() + mockkObject(DuplicateDoesNotOverrideConfig.SECURITY_CONTEXT_REPOSITORY) every { DuplicateDoesNotOverrideConfig.SECURITY_CONTEXT_REPOSITORY.loadContext(any()) } returns mockk(relaxed = true) mvc.perform(get("/")) verify(exactly = 1) { DuplicateDoesNotOverrideConfig.SECURITY_CONTEXT_REPOSITORY.loadContext(any()) } } - @EnableWebSecurity open class DuplicateDoesNotOverrideConfig : WebSecurityConfigurerAdapter() { override fun configure(http: HttpSecurity) { @@ -102,7 +75,7 @@ class SecurityContextDslTests { } companion object { - var SECURITY_CONTEXT_REPOSITORY = mockk(relaxed = true) + val SECURITY_CONTEXT_REPOSITORY = NullSecurityContextRepository() } }