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
@Singleton | |
@Component(modules = [DaggerModule::class]) | |
interface AppComponent { | |
fun inject(mainActivity: MainActivity) | |
@Component.Factory | |
interface Factory { | |
fun create(@BindsInstance welcomeMessage: String): AppComponent |
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 DaggerApplication : Application() { | |
val appComponent: AppComponent by lazy { | |
DaggerAppComponent.factory().create("welcome to Dagger") | |
} | |
} |
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() { | |
@Inject | |
lateinit var greetingService: GreetingService | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
(application as DaggerApplication).appComponent.inject(this) |
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
@RunWith(MockitoJUnitRunner::class) | |
class GreetingServiceTest { | |
private lateinit var greetingService: GreetingService | |
@Mock | |
lateinit var timeService: TimeService | |
@Before | |
fun setup() { |
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
@Singleton | |
class GreetingServiceImpl @Inject constructor(private val messageData: MessageData, private val timeService: TimeService) : | |
GreetingService { | |
@Singleton | |
class TimeServiceImpl @Inject constructor() : TimeService { | |
data class MessageData @Inject constructor(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 toothpickModule = module { | |
bind<GreetingService>().toClass<GreetingServiceImpl>().singleton() | |
bind<TimeService>().toInstance(TimeServiceImpl()) | |
bind<MessageData>().toInstance(MessageData()) | |
} |
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
@InjectConstructor | |
class GreetingServiceImpl(private val messageData: MessageData, private val timeService: TimeService) : | |
GreetingService { |
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 ToothpickApplication : Application() { | |
override fun onCreate() { | |
super.onCreate() | |
KTP.openRootScope().installModules(toothpickModule) | |
} | |
} |
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: GreetingService by inject() | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
KTP.openRootScope().inject(this) |
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
@RunWith(MockitoJUnitRunner::class) | |
class GreetingServiceMockTest { | |
@get:Rule | |
var toothPickRule = ToothPickRule(this, "scope") | |
@Inject | |
lateinit var greetingService: GreetingService | |
@Mock |