Skip to content

Commit ef1b033

Browse files
denis-zhdanovKevinGilmore
authored andcommitted
BAEL-1462 Kotlin DI with Kodein (eugenp#3544)
* BAEL-1462 Kotlin DI with Kodein * BAEL-1462 Kotlin DI with Kodein * applied editor's suggestions * removed unnecessary curly braces * Moved kodein files into core-kotlin as per editor's review * Using assertj instead of junit assertions as per editor's instruction
1 parent 30285c1 commit ef1b033

7 files changed

Lines changed: 220 additions & 1 deletion

File tree

‎core-kotlin/pom.xml‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,18 @@
7676
<artifactId>mockito-kotlin</artifactId>
7777
<version>${mockito-kotlin.version}</version>
7878
<scope>test</scope>
79-
</dependency>
79+
</dependency>
80+
<dependency>
81+
<groupId>com.github.salomonbrys.kodein</groupId>
82+
<artifactId>kodein</artifactId>
83+
<version>${kodein.version}</version>
84+
</dependency>
85+
<dependency>
86+
<groupId>org.assertj</groupId>
87+
<artifactId>assertj-core</artifactId>
88+
<version>${assertj.version}</version>
89+
<scope>test</scope>
90+
</dependency>
8091
</dependencies>
8192

8293
<build>
@@ -189,11 +200,13 @@
189200
<kotlin-reflect.version>1.1.2</kotlin-reflect.version>
190201
<kotlinx.version>0.15</kotlinx.version>
191202
<mockito-kotlin.version>1.5.0</mockito-kotlin.version>
203+
<kodein.version>4.1.0</kodein.version>
192204

193205
<junit.jupiter.version>5.0.0</junit.jupiter.version>
194206
<junit.platform.version>1.0.0</junit.platform.version>
195207
<junit.vintage.version>4.12.0</junit.vintage.version>
196208
<junit4.version>4.12</junit4.version>
209+
<assertj.version>3.9.1</assertj.version>
197210
</properties>
198211

199212
</project>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package com.baeldung.kotlin.kodein
2+
3+
class Controller(private val service : Service)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package com.baeldung.kotlin.kodein
2+
3+
interface Dao
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package com.baeldung.kotlin.kodein
2+
3+
class JdbcDao : Dao
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package com.baeldung.kotlin.kodein
2+
3+
class MongoDao : Dao
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package com.baeldung.kotlin.kodein
2+
3+
class Service(private val dao: Dao, private val tag: String)
Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
package com.baeldung.kotlin.kodein
2+
3+
import com.github.salomonbrys.kodein.*
4+
import org.assertj.core.api.Assertions.assertThat
5+
import org.junit.Test
6+
7+
class KodeinUnitTest {
8+
9+
class InMemoryDao : Dao
10+
11+
@Test
12+
fun whenSingletonBinding_thenSingleInstanceIsCreated() {
13+
var created = false
14+
val kodein = Kodein {
15+
bind<Dao>() with singleton {
16+
created = true
17+
MongoDao()
18+
}
19+
}
20+
21+
assertThat(created).isFalse()
22+
23+
val dao1: Dao = kodein.instance()
24+
25+
assertThat(created).isTrue()
26+
27+
val dao2: Dao = kodein.instance()
28+
29+
assertThat(dao1).isSameAs(dao2)
30+
}
31+
32+
@Test
33+
fun whenFactoryBinding_thenNewInstanceIsCreated() {
34+
val kodein = Kodein {
35+
bind<Dao>() with singleton { MongoDao() }
36+
bind<Service>() with factory { tag: String -> Service(instance(), tag) }
37+
}
38+
val service1: Service = kodein.with("myTag").instance()
39+
val service2: Service = kodein.with("myTag").instance()
40+
41+
assertThat(service1).isNotSameAs(service2)
42+
}
43+
44+
@Test
45+
fun whenProviderBinding_thenNewInstanceIsCreated() {
46+
val kodein = Kodein {
47+
bind<Dao>() with provider { MongoDao() }
48+
}
49+
val dao1: Dao = kodein.instance()
50+
val dao2: Dao = kodein.instance()
51+
52+
assertThat(dao1).isNotSameAs(dao2)
53+
}
54+
55+
@Test
56+
fun whenTaggedBinding_thenMultipleInstancesOfSameTypeCanBeRegistered() {
57+
val kodein = Kodein {
58+
bind<Dao>("dao1") with singleton { MongoDao() }
59+
bind<Dao>("dao2") with singleton { MongoDao() }
60+
}
61+
val dao1: Dao = kodein.instance("dao1")
62+
val dao2: Dao = kodein.instance("dao2")
63+
64+
assertThat(dao1).isNotSameAs(dao2)
65+
}
66+
67+
@Test
68+
fun whenEagerSingletonBinding_thenCreationIsEager() {
69+
var created = false
70+
val kodein = Kodein {
71+
bind<Dao>() with eagerSingleton {
72+
created = true
73+
MongoDao()
74+
}
75+
}
76+
77+
assertThat(created).isTrue()
78+
val dao1: Dao = kodein.instance()
79+
val dao2: Dao = kodein.instance()
80+
81+
assertThat(dao1).isSameAs(dao2)
82+
}
83+
84+
@Test
85+
fun whenMultitonBinding_thenInstancesAreReused() {
86+
val kodein = Kodein {
87+
bind<Dao>() with singleton { MongoDao() }
88+
bind<Service>() with multiton { tag: String -> Service(instance(), tag) }
89+
}
90+
val service1: Service = kodein.with("myTag").instance()
91+
val service2: Service = kodein.with("myTag").instance()
92+
93+
assertThat(service1).isSameAs(service2)
94+
}
95+
96+
@Test
97+
fun whenInstanceBinding_thenItIsReused() {
98+
val dao = MongoDao()
99+
val kodein = Kodein {
100+
bind<Dao>() with instance(dao)
101+
}
102+
val fromContainer: Dao = kodein.instance()
103+
104+
assertThat(dao).isSameAs(fromContainer)
105+
}
106+
107+
@Test
108+
fun whenConstantBinding_thenItIsAvailable() {
109+
val kodein = Kodein {
110+
constant("magic") with 42
111+
}
112+
val fromContainer: Int = kodein.instance("magic")
113+
114+
assertThat(fromContainer).isEqualTo(42)
115+
}
116+
117+
@Test
118+
fun whenUsingModules_thenTransitiveDependenciesAreSuccessfullyResolved() {
119+
val jdbcModule = Kodein.Module {
120+
bind<Dao>() with singleton { JdbcDao() }
121+
}
122+
val kodein = Kodein {
123+
import(jdbcModule)
124+
bind<Controller>() with singleton { Controller(instance()) }
125+
bind<Service>() with singleton { Service(instance(), "myService") }
126+
}
127+
128+
val dao: Dao = kodein.instance()
129+
assertThat(dao).isInstanceOf(JdbcDao::class.java)
130+
}
131+
132+
@Test
133+
fun whenComposition_thenBeansAreReUsed() {
134+
val persistenceContainer = Kodein {
135+
bind<Dao>() with singleton { MongoDao() }
136+
}
137+
val serviceContainer = Kodein {
138+
extend(persistenceContainer)
139+
bind<Service>() with singleton { Service(instance(), "myService") }
140+
}
141+
val fromPersistence: Dao = persistenceContainer.instance()
142+
val fromService: Dao = serviceContainer.instance()
143+
144+
assertThat(fromPersistence).isSameAs(fromService)
145+
}
146+
147+
@Test
148+
fun whenOverriding_thenRightBeanIsUsed() {
149+
val commonModule = Kodein.Module {
150+
bind<Dao>() with singleton { MongoDao() }
151+
bind<Service>() with singleton { Service(instance(), "myService") }
152+
}
153+
val testContainer = Kodein {
154+
import(commonModule)
155+
bind<Dao>(overrides = true) with singleton { InMemoryDao() }
156+
}
157+
val dao: Dao = testContainer.instance()
158+
159+
assertThat(dao).isInstanceOf(InMemoryDao::class.java)
160+
}
161+
162+
@Test
163+
fun whenMultiBinding_thenWorks() {
164+
val kodein = Kodein {
165+
bind() from setBinding<Dao>()
166+
bind<Dao>().inSet() with singleton { MongoDao() }
167+
bind<Dao>().inSet() with singleton { JdbcDao() }
168+
}
169+
val daos: Set<Dao> = kodein.instance()
170+
171+
assertThat(daos.map { it.javaClass as Class<*> }).containsOnly(MongoDao::class.java, JdbcDao::class.java)
172+
}
173+
174+
@Test
175+
fun whenInjector_thenWorks() {
176+
class Controller2 {
177+
private val injector = KodeinInjector()
178+
val service: Service by injector.instance()
179+
fun injectDependencies(kodein: Kodein) = injector.inject(kodein)
180+
}
181+
182+
val kodein = Kodein {
183+
bind<Dao>() with singleton { MongoDao() }
184+
bind<Service>() with singleton { Service(instance(), "myService") }
185+
}
186+
val controller = Controller2()
187+
controller.injectDependencies(kodein)
188+
189+
assertThat(controller.service).isNotNull
190+
}
191+
}

0 commit comments

Comments
 (0)