我正在尝试使用 Spock 和 ConfigurationProperties。
但是在我的单元测试中,模拟 @ConfigurationProperties 对我不起作用。
属性类
@ConfigurationProperties(prefix = "jwt")
@ConstructorBinding
class JwtProperties(
val secretKey: String,
val accessTokenExp: Long,
val refreshTokenExp: Long
) {
companion object {
const val TOKEN_PREFIX = "Bearer "
const val TOKEN_HEADER_NAME = "Authorization"
const val ACCESS_VALUE = "access"
const val REFRESH_VALUE = "refresh"
}
}
测试班
class JwtTokenProviderTest extends Specification {
private JwtProperties jwtProperties = GroovyMock(JwtProperties)
private AuthDetailsService authDetailsService = GroovyMock(AuthDetailsService)
private JwtTokenProvider jwtTokenProvider = new JwtTokenProvider(authDetailsService, jwtProperties)
def "AuthenticateUser Success"() {
given:
jwtProperties.getSecretKey() >> "asdfdsaf"
jwtProperties.getAccessTokenExp() >> 100000
def bearerToken = jwtTokenProvider.getAccessToken("email").accessToken
def accessToken = jwtTokenProvider.parseToken(bearerToken)
authDetailsService.loadUserByUsername("email") >> new AuthDetails(new User())
when:
jwtTokenProvider.authenticateUser(accessToken)
then:
noExceptionThrown()
.
.
.
但是当我以调试模式运行测试时,JwtProperties 的字段从未初始化。