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?