Skip to content
Permalink
Browse files

Add support to Koin (#907)

Closes #515
  • Loading branch information...
Kerooker committed Jul 20, 2019
1 parent b707035 commit ee70fb688fdbc335acf8feac4008d3742887cbd3
@@ -28,6 +28,7 @@ allprojects {

repositories {
mavenCentral()
jcenter()
}

sourceCompatibility = 1.6
@@ -0,0 +1,26 @@
plugins {
id 'java'
id 'org.jetbrains.kotlin.jvm'
id 'java-library'
id 'maven-publish'
id 'signing'
}

dependencies {
compile project(':kotlintest-core')
implementation "org.koin:koin-core:2.0.1"
implementation("org.koin:koin-test:2.0.1") {
exclude group: "junit", module: "junit"
}

testCompile project(':kotlintest-runner:kotlintest-runner-junit5')
}

test {
useJUnitPlatform()
testLogging {
events "PASSED", "FAILED", "SKIPPED", "STANDARD_OUT", "STANDARD_ERROR"
}
}

apply from: '../../publish.gradle'
@@ -0,0 +1,25 @@
package io.kotlintest.koin

import io.kotlintest.TestCase
import io.kotlintest.TestResult
import io.kotlintest.extensions.TestListener
import org.koin.core.context.startKoin
import org.koin.core.context.stopKoin
import org.koin.core.module.Module

class KoinListener(
private val modules: List<Module>
) : TestListener {

constructor(module: Module) : this(listOf(module))

override fun beforeTest(testCase: TestCase) {
startKoin {
modules(modules)
}
}

override fun afterTest(testCase: TestCase, result: TestResult) {
stopKoin()
}
}
@@ -0,0 +1,19 @@
package com.sksamuel.kt.koin

import org.koin.dsl.module

class GenericRepository {
fun foo() = "Bar"
}

class GenericService(
val repository: GenericRepository
) {

fun foo() = repository.foo()
}

val koinModule = module {
single { GenericService(get()) }
single { GenericRepository() }
}
@@ -0,0 +1,37 @@
package com.sksamuel.kt.koin

import io.kotlintest.*
import io.kotlintest.koin.KoinListener
import io.kotlintest.specs.FunSpec
import org.koin.core.context.GlobalContext
import org.koin.test.KoinTest
import org.koin.test.inject
import org.koin.test.mock.declareMock
import org.mockito.BDDMockito

class KotlinListenerTest : FunSpec(), KoinTest {

override fun listeners() = listOf(KoinListener(koinModule))

private val genericService by inject<GenericService>()

init {
test("Should have autowired the service correctly") {
genericService.foo() shouldBe "Bar"
}

test("Should allow mocking correctly") {
declareMock<GenericRepository> {
BDDMockito.given(foo()).willReturn("DootyDoot")
}

genericService.foo() shouldBe "DootyDoot"
}
}

override fun afterSpecClass(spec: Spec, results: Map<TestCase, TestResult>) {
GlobalContext.getOrNull() shouldBe null // We should finish koin after test execution
}

override fun isolationMode() = IsolationMode.InstancePerTest
}
@@ -8,6 +8,7 @@ include 'kotlintest-core',
'kotlintest-runner:kotlintest-runner-junit4',
'kotlintest-runner:kotlintest-runner-junit5',
'kotlintest-extensions:kotlintest-extensions-spring',
'kotlintest-extensions:kotlintest-extensions-koin',
'kotlintest-extensions:kotlintest-extensions-allure',
'kotlintest-plugins:kotlintest-plugins-pitest',
'kotlintest-tests:kotlintest-tests-core',

0 comments on commit ee70fb6

Please sign in to comment.
You can’t perform that action at this time.