Last active
October 31, 2020 19:51
-
-
Save taylorsloan/b9f207eb2fc639ea856cf26f8c3f3c65 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.junit.jupiter.api.extension.* | |
import org.koin.core.Koin | |
import org.koin.core.context.startKoin | |
import org.koin.core.context.stopKoin | |
import org.koin.dsl.KoinAppDeclaration | |
/** | |
* Usage: | |
* Add as companion to test class like | |
* class GreetingDatabaseImplTest: KoinTest { | |
* companion object { | |
* @JvmField | |
* @RegisterExtension | |
* var extension: KoinTestExtension = KoinTestExtension.create { | |
* modules(loggingDependencyModule()) | |
* } | |
* } | |
* } | |
* | |
* [KoinTestExtension] which will automatically start and stop Koin. | |
* @author Taylor Sloan based on KoinTestRule written by Nick Cipollo | |
*/ | |
class KoinTestExtension constructor(private val appDeclaration: KoinAppDeclaration) : AfterEachCallback, BeforeEachCallback { | |
var _koin: Koin? = null | |
val koin: Koin | |
get() = _koin ?: error("No Koin application found") | |
override fun beforeEach(context: ExtensionContext?) { | |
_koin = startKoin(appDeclaration = appDeclaration).koin | |
} | |
override fun afterEach(context: ExtensionContext?) { | |
stopKoin() | |
_koin = null | |
} | |
companion object { | |
fun create(appDeclaration: KoinAppDeclaration): KoinTestExtension { | |
return KoinTestExtension(appDeclaration) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment