Reputation: 1
I am having trouble using my application.properties values while testing.
Here's my code:
@Repository
class RedisSubscriptionStore(): SubscriptionStore {
@Value("\${default.package}")
lateinit var defaultPackageForFCMInstance: String }
and this works as expected. It's used in my updateSubscription method with the correct value from application.properties
The problem is in my test when I use the class the value is not instantiated and throws an error
@SpringBootTest()
@Tag("integration")
internal class RedisSubscriptionStoreIntegTest @Autowired constructor(
val objectMapper: ObjectMapper,
val redisTemplate: RedisTemplate<String, String>
) {
val sut = RedisSubscriptionStore(redisTemplate, objectMapper)
@Test
fun `return 200 when updating subscription`() {
sut.updateSubscription(updatedSub)
//Assert
val newlyUpdatedSub = getSubscription(updatedSub.peerID)
Assertions.assertEquals(updatedSub, newlyUpdatedSub)
}
but this throws the error: lateinit property defaultPackageForFCMInstance has not been initialized
even though this works with the correct value:
@SpringBootTest()
@Tag("integration")
internal class RedisSubscriptionStoreIntegTest @Autowired constructor(
val objectMapper: ObjectMapper,
val redisTemplate: RedisTemplate<String, String>
) {
@Value("\${default.package}")
lateinit var defaultPackageForFCMInstance: String
}
so why is my defaultPackageForFCMInstance not initialized when calling from my test class? It obviously have a value, I've tried printing it out in the test class before calling it from sut
Upvotes: 0
Views: 983
Reputation: 36143
Your are instantiating the RedisSubscriptionStore
So there is no way for Spring to inject value. Either also pass the value in the constructor or use injecation in the test
@Autowired
var sut: RedisSubscriptionStore
Upvotes: 1