Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to inject Spring component with @Value annotation into test?

I have a Kotlin Spring Boot app with a few components in it that use the @Value annotation to pull values from my application.yaml. Those components look like this:

@Component
class IosClientConfig : ClientConfig {
    @Value("\${<spring-config-value>}")
    override lateinit var redirectUri: String

    @Value("\${<spring-config-value>}")
    override lateinit var clientId: String

    @Value("\${<spring-config-value>}")
    override lateinit var clientSecret: String

    @Value("\${<spring-config-value>}")
    override lateinit var tokenExchangeEndpoint: String
}

Now I inject these config objects into the service layer which looks like this:

@Service
class TokenExchangeService {

    val logger = LoggerFactory.getLogger(javaClass)

    @Autowired
    lateinit var iOSConfig: IosClientConfig

    @Autowired
    lateinit var androidConfig: AndroidClientConfig
    ...
}

The problem arises when I want to write unit tests for this TokenExchangeService. The IosClientConfig and the AndroidClientConfig components can't be automatically injected without setting up the entire Spring application context, but that would defeat the entire purpose of the unit test, so I can't Autowire the service class in the test. And I can't inject them in the constructor of the service class since lateinit vars can't be passed into the primary constructor. Something like this would be the desired outcome, but this won't work without configuring the entire application context.

@ExtendWith(MockKExtension::class)
class TokenExchangeServiceTest() {

    @Autowired
    lateinit var tokenExchangeService: TokenExchangeService

    @BeforeEach
    fun init() {
        MockKAnnotations.init(this)
    }

Is there a better way to inject these components into the service class that will make them more testable? Or is there a way to setup the test such that I can successfully Autowire the service class without configuring the entire application context?

like image 288
D-Wire Avatar asked Dec 22 '25 09:12

D-Wire


1 Answers

I think this should work for you:

@Service
class TokenExchangeService(
    private val iOSConfig: IosClientConfig
    private val androidConfig: AndroidClientConfig
) {
    val logger = LoggerFactory.getLogger(javaClass)
    ...
}

because Spring matches the TokenExchangeService constructor args to the Components like IosClientConfig and automatically wires without need for any annotations or late-binding.

Now, with constructor parameters you can proceed with your test and inject those ClientConfig classes (or mocks)


OP then asked

What would be the best way to inject the actual ClientConfigs instead of mocks into the service constructor in the test, while also making sure the @Value annotations are being populated in time?

From you original question you said that you wanted to avoid "setting up the entire Spring application context". So what do you mean about using the "actual ClientConfigs"?

In general I try to avoid too many tests instantiating Spring due to the time penalty - you could deserialise the ClientConfigs from JSON much faster, I'd think. But anyways you can mix and match easily enough, something like this given my constructor-arg variation of the TokenExchangeService above:

@SpringBootTest
class TokenExchangeServiceTest {
    @Autowired
    private lateinit var iOSConfig: IosClientConfig
    @Autowired
    private lateinit var androidConfig: AndroidClientConfig

    private lateinit var tokenExchangeService: TokenExchangeService

    @BeforeEach
    fun init() {
        tokenExchangeService = TokenExchangeService(iOSConfig, androidConfig)
    }
}
like image 73
AndrewL Avatar answered Dec 23 '25 22:12

AndrewL



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!