我想测试没有 @SpringBootTest 注释的 Properties 类。但是它不起作用。我该如何修复它?我使用的是 Kotlin,Spring Boot 2.7.18
@ActiveProfiles("test")
@ExtendWith(SpringExtension::class)
@ContextConfiguration(classes = [TestConfig::class])
class AESTest {
@Autowired
lateinit var properties: TestAESProperty
@Test
fun decrypt() {
assertEquals(properties.key, "hello")
assertEquals(properties.iv, "world")
}
}
@TestConfiguration
@EnableConfigurationProperties(TestAESProperty::class)
class TestConfig {
}
@ConstructorBinding
@ConfigurationProperties(prefix = "common.test.aes")
data class TestAESProperty(
val key: String,
val iv: String,
)
---- 应用程序测试.yml ----------
spring:
config:
activate:
on-profile: test
common:
test:
aes:
key: hello
iv: world
但是,出现了以下错误。
Failed to bind properties under 'common.test.aes' to TestAESProperty
Parameter specified as non-null is null: method TestAESProperty.<init>, parameter key
Error creating bean with name 'TestAESProperty'
Could not bind properties to 'TestAESProperty' : prefix=common.test.aes, ignoreInvalidFields=false, ignoreUnknownFields=true; nested exception is org.springframework.boot.context.properties.bind.BindException: Failed to bind properties under 'common.test.aes' to TestAESProperty
我想从“application-test.yml”文件进行数据绑定,因为使用 AWS 服务管理器可以更加安全。