} } fun retrofitModule(baseUrl: String) = Kodein.Module { bind<Retrofit>() with singleton { Retrofit.Builder() .baseUrl(baseUrl) .build() } } Módulos en Kodein Pueden depender unos de otros. No hay que preocuparse del orden de inicialización.
Kodein.lazy { import(androidModule(this@App)) import(retrofitModule(baseUrl) } } // No Android val kodein = Kodein { import(…) import(…) } Uso de módulos
Kodein.lazy { import(androidModule(this@App)) import(retrofitModule(baseUrl) } } // No Android val kodein = Kodein { import(…) import(…) } Uso de módulos
closestKodein() private val random: Random by kodein.instance() } Obtención de las dependencias • closestKodein() funciona directamente en cualquier clase Android. • Se usará closestKodein(context) para clases no Android
tener diferentes contenedores Kodein. • ConfigurableKodein permite añadir y quitar módulos dinámicamente: uso en tests. • Karumi ofrece un ejemplo en https://github.com/Karumi/ kodein-sample-testing Tests unitarios y de instrumentación
verbosidad. • Aprendizaje más fácil para programadores no experimentados en DI. • Inconvenientes: • Se pierde la validación en tiempo de compilación. • Mayor coste de integración en código Java. Kodein vs Dagger
get an album list with two items") { assert(albums.size == 2) } it("should get expected first item in albums list") { assert(albums[0].id == lastFmValidAlbums[0].mbid) } it("should get expected second item in albums list") { assert(albums[1].id == lastFmValidAlbums[1].mbid) } } Ejemplo en Spek
String) = Result( value.startsWith("a"), "String $value should start with a", "String $value should not start with a” )} "abc" should startsWithA() "xyz" shouldNot startsWithA() Custom matchers
permite aumentar la legibilidad de los tests. • jUnit 5 (y todos los frameworks basados en él) ofrecen posibilidades mucho más potentes que jUnit 4. Conclusiones