Dagger 2 for Dummies in Kotlin — Testing

Dagger 2 is so magically that it injects the member variable. Magic is good.

But… So how do I do testing? How can I Mock those variable? As I am a dummy programmer, testing is even more important for me…

Fear not… it should be simple as Dagger 2 uses the same magic trick.

Before you proceed, just in case you don’t know, this is actually the continue of previous section. Do check it out if you haven’t read them.

From where I left previously

As usual, I like to continue where I left with the simple single page Dagger 2 code.

const val LOVE = "Love"
const val HELLO = "Hello"

class MainActivity : AppCompatActivity() {

@Inject @field:Choose(LOVE) lateinit var infoLove: Info
@Inject @field:Choose(HELLO) lateinit var infoHello: Info

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
DaggerMagicBox.create().poke(this)
text_view.text = "${infoLove.text}…

--

--