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