Fix Kotlin mockk test compatibility

Issue gh-11039
This commit is contained in:
Eleftheria Stein 2022-04-26 18:13:29 +02:00
parent 558bb161c5
commit 7dc4364f43
1 changed files with 3 additions and 30 deletions

View File

@ -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<SecurityContextPersistenceFilter>()) }
}
@EnableWebSecurity
open class ObjectPostProcessorConfig : WebSecurityConfigurerAdapter() {
override fun configure(http: HttpSecurity) {
// @formatter:off
http {
securityContext { }
}
// @formatter:on
}
@Bean
open fun objectPostProcessor(): ObjectPostProcessor<Any> = objectPostProcessor
companion object {
var objectPostProcessor: ObjectPostProcessor<Any> = spyk(ReflectingObjectPostProcessor())
class ReflectingObjectPostProcessor : ObjectPostProcessor<Any> {
override fun <O> 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<HttpRequestResponseHolder>()) } returns mockk<SecurityContext>(relaxed = true)
mvc.perform(get("/"))
verify(exactly = 1) { DuplicateDoesNotOverrideConfig.SECURITY_CONTEXT_REPOSITORY.loadContext(any<HttpRequestResponseHolder>()) }
}
@EnableWebSecurity
open class DuplicateDoesNotOverrideConfig : WebSecurityConfigurerAdapter() {
override fun configure(http: HttpSecurity) {
@ -102,7 +75,7 @@ class SecurityContextDslTests {
}
companion object {
var SECURITY_CONTEXT_REPOSITORY = mockk<SecurityContextRepository>(relaxed = true)
val SECURITY_CONTEXT_REPOSITORY = NullSecurityContextRepository()
}
}