This file contains hidden or 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
data class MessageData(val welcomeMessage: String) |
This file contains hidden or 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
val koinModule = module { | |
single { MessageData() } | |
single<TimeService> { TimeServiceImpl() } | |
single<GreetingService> { | |
GreetingServiceImpl(get(), get()) | |
} | |
} |
This file contains hidden or 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
class KoinApplication : Application() { | |
override fun onCreate() { | |
super.onCreate() | |
startKoin { | |
modules(koinModule) | |
} | |
} | |
} |
This file contains hidden or 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
class MainActivity : AppCompatActivity() { | |
private val greetingService by inject<GreetingService>() | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
val greetings = findViewById<TextView>(R.id.greetingsTextView) | |
greetings.text = "${greetingService.greetings()}" |
This file contains hidden or 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
class GreetingServiceMockTest : KoinTest { | |
private val greetingService: GreetingService by inject() | |
@Test | |
fun testGreetingInTheMorning() { | |
startKoin { modules(koinModule) } | |
declareMock<TimeService> { | |
given(this.getHourOfDay()).willReturn(9) | |
} |
This file contains hidden or 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
val kodeinModule = Kodein.Module(name = "appmodule") { | |
bind() from singleton { MessageData() } | |
bind<TimeService>() with singleton { TimeServiceImpl() } | |
bind<GreetingService>() with singleton { | |
GreetingServiceImpl(instance(), instance()) } | |
} |
This file contains hidden or 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
class KodeinApplication : Application(), KodeinAware { | |
override val kodein = Kodein.lazy { | |
import(kodeinModule) | |
} | |
} |
This file contains hidden or 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
class MainActivity : AppCompatActivity(), KodeinAware { | |
override val kodein by closestKodein() | |
private val greetingService by instance<GreetingService>() | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) |
This file contains hidden or 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
class GreetingServiceTest { | |
@Test | |
fun testGreetingInTheMorning() { | |
val testContainer = Kodein { | |
import(kodeinModule) | |
bind<TimeService>(overrides = true) with singleton { | |
object : TimeService { | |
override fun getHourOfDay(): Int = 9 |
This file contains hidden or 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
@Module | |
interface DaggerModule { | |
@Binds | |
fun provideTimeService(impl: TimeServiceImpl): TimeService | |
@Binds | |
fun provideGreetingSersvice(impl: GreetingServiceImpl): GreetingService | |
} |