Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

437
Visualizações
How can I use Koin to inject in a @BeforeClass static method?

I have an integration test that needs to call a REST service to get an access token one time before any subsequent tests are run. Before adding Koin to my project, I accomplished this in a static method annotated with @BeforeClass like so:

class PersonRepositoryIntegrationTest {

    companion object {
        private var _clientToken: String? = null

        @BeforeClass
        @JvmStatic
        fun setup() {
            _clientToken = AuthRepository().getClientToken()!!.accessToken
        }
    }

    @Test
    fun testCreatePerson() {
        PersonRepository().createPerson(_clientToken)
    }

AuthRepository and PersonRepository have additional dependencies that up until now were instantiated in their constructors. Now, I want to use Koin to resolve these dependencies by injecting the repositories:

class PersonRepositoryIntegrationTest : KoinTest {

    companion object {
        private val _authRepository by inject<IAuthRepository>()
        private val _personRepository by inject<IPersonRepository>()
        private var _clientToken: String? = null

        @BeforeClass
        @JvmStatic
        fun beforeClass() {
            startKoin(listOf(AppModule.appModule))
            _clientToken = _authRepository.getClientToken()!!.accessToken
        }
    }

When I try to use inject inside the companion object, the compiler gives an error:

Unresolved reference.
None of the following candidates is applicable because of receiver type mismatch.

* public inline fun <reified T : Any> KoinComponent.inject(name: String = ..., scope: Scope? = ..., noinline parameters: ParameterDefinition = ...): Lazy<IAuthRepository> defined in org.koin.standalone

Is there another way I can use Koin to inject my classes in a @BeforeClass static method like this?

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

According to kotlin documentation, companion objects are technically real objects.

even though the members of companion objects look like static members in other languages, at runtime those are still instance members of real objects, and can, for example, implement interfaces:

If a class wants to inject dependencies and it is not one of the koin supported classes(Activity, Fragment, ViewModel, KoinTest etc), then that class should implement the KoinComponent interface.

So consider changing your companion object definition to the following and try again.

companion object : KoinComponent{
        private val _authRepository by inject<IAuthRepository>()
        private val _personRepository by inject<IPersonRepository>()
        private var _clientToken: String? = null

        @BeforeClass
        @JvmStatic
        fun beforeClass() {
            startKoin(listOf(AppModule.appModule))
            _clientToken = _authRepository.getClientToken()!!.accessToken
        }
over 4 years ago · Santiago Trujillo Relatório

0

In addition to the accepted answer, I discovered that I can use the inject method from org.koin.java.standalone.KoinJavaComponent, documented here:

import org.koin.java.standalone.KoinJavaComponent.inject

class PersonRepositoryIntegrationTest : KoinTest {

    companion object {
        private val _authRepository by inject(IAuthRepository::class.java)
        private val _personRepository by inject(IPersonRepository::class.java)
        private var _clientToken: String? = null

        @BeforeClass
        @JvmStatic
        fun beforeClass() {
            startKoin(listOf(AppModule.appModule))
            _clientToken = _authRepository.getClientToken()!!.accessToken
        }
    }

This seems strange to me because I'm using Java interop methods in a Kotlin class, so I'd prefer to solve the problem by changing my companion object to extend KoinComponent instead as recommended here.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda