Java Mockk-模拟实现多个接口的最终类时的ClassCastException
我尝试使用这个Java类的模拟:Java Mockk-模拟实现多个接口的最终类时的ClassCastException,java,spring,spring-security,kotlin,mockk,Java,Spring,Spring Security,Kotlin,Mockk,我尝试使用这个Java类的模拟: public final class HttpSecurity extends AbstractConfiguredSecurityBuilder<DefaultSecurityFilterChain, HttpSecurity> implements SecurityBuilder<DefaultSecurityFilterChain>, HttpSecurityBuilder<Ht
public final class HttpSecurity extends
AbstractConfiguredSecurityBuilder<DefaultSecurityFilterChain, HttpSecurity>
implements SecurityBuilder<DefaultSecurityFilterChain>,
HttpSecurityBuilder<HttpSecurity>
为了测试这段Java代码:
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable().
headers().frameOptions().disable().and()
.formLogin().loginPage("/login").permitAll()....etc
当我尝试使用它时,我得到了以下错误
java.lang.ClassCastException: org.springframework.security.config.annotation.web.HttpSecurityBuilder$Subclass2 cannot be cast to org.springframework.security.config.annotation.web.builders.HttpSecurity
这里的测试类:
package com.whatever
import io.mockk.mockk
import io.mockk.mockkClass
import org.junit.jupiter.api.Test
import org.springframework.core.env.Environment
import org.springframework.security.authentication.AuthenticationManager
import org.springframework.security.config.annotation.web.builders.HttpSecurity
internal class SecurityConfigTest {
private val authManager: AuthenticationManager = mockk()
val env : Environment = mockk()
private val httpSecurity: HttpSecurity = mockk(relaxed = true)
val securityConfig : SecurityConfig = SecurityConfig(authManager,env)
@Test
fun configure() {
securityConfig.configure(httpSecurity)
}
}
有没有办法解决这个问题?这里的问题是模板参数类型被删除,无法恢复。唯一的解决方案是直接指定mock,以便
具体化
类型将捕获实际的类:
val mockk = mockk<HttpSecurity>(relaxed = true)
val csrf = mockk.csrf()
every { csrf.disable() } returns mockk(relaxed = true)
val disable = csrf.disable()
disable.headers()
val mockk=mockk(relaxed=true)
val csrf=mock.csrf()
每个{csrf.disable()}返回mock(relaxed=true)
val disable=csrf.disable()
禁用.headers()
似乎mock
创建了一个类型作为生成器方法的宽松返回值。尝试手动模拟每个方法以从那里返回HttpSecurity
。谢谢,我想我可能可以不这样做而逃脱,但我想不会:-)添加了一个问题
val mockk = mockk<HttpSecurity>(relaxed = true)
val csrf = mockk.csrf()
every { csrf.disable() } returns mockk(relaxed = true)
val disable = csrf.disable()
disable.headers()