1
+ package org.jlleitschuh.guice
2
+
3
+ import com.google.inject.AbstractModule
4
+ import com.google.inject.Guice
5
+ import org.junit.jupiter.api.Assertions.assertSame
6
+ import org.junit.jupiter.api.Assertions.assertTrue
7
+ import org.junit.jupiter.api.Test
8
+
9
+ class ModuleTest {
10
+ interface Interface
11
+
12
+ class Implementation : Interface
13
+
14
+ @Test
15
+ fun `simple module` () {
16
+ val simpleModule = module {
17
+ bind(key<Interface >()).to(key<Implementation >())
18
+ }
19
+ val injector = Guice .createInjector(simpleModule)
20
+ val theInterface = injector.getInstance(key<Interface >())
21
+ assertTrue(theInterface is Implementation )
22
+ }
23
+
24
+ @Test
25
+ fun `module not using key` () {
26
+ val simpleModule = module {
27
+ bind(Interface ::class ).to(Implementation ::class )
28
+ }
29
+ val injector = Guice .createInjector(simpleModule)
30
+ val theInterface = injector.getInstance(key<Interface >())
31
+ assertTrue(theInterface is Implementation )
32
+ }
33
+
34
+ @Test
35
+ fun `module using instance` () {
36
+ val instance = Implementation ()
37
+ val simpleModule = module {
38
+ bind(Interface ::class ).toInstance(instance)
39
+ }
40
+ val injector = Guice .createInjector(simpleModule)
41
+ val theInterface = injector.getInstance(key<Interface >())
42
+ assertSame(instance, theInterface)
43
+ }
44
+
45
+ @Test
46
+ fun `complicated module` () {
47
+ val complicated = object : AbstractModule () {
48
+ override fun configure () {
49
+ bind(key<Interface >()).to(key<Implementation >())
50
+ }
51
+ }
52
+ val injector = Guice .createInjector(complicated)
53
+ injector.getInstance(key<Interface >())
54
+ }
55
+
56
+ }
0 commit comments