Upgrade to Pro — share decks privately, control downloads, hide ads and more …

mockito-kotlin

 mockito-kotlin

Android Test Night #2

Keisuke Kobayashi

December 15, 2017
Tweet

More Decks by Keisuke Kobayashi

Other Decks in Programming

Transcript

  1. mockito-kotlin
    Keisuke Kobayashi
    Android Test Night #2

    View full-size slide

  2. About Me
    • Keisuke Kobayashi
    • GitHub, QIita: kobakei
    • Twitter: kobakei122
    • Kyash, Inc
    • ݸਓࣄۀओ

    View full-size slide

  3. mockito-kotlin͸͍͍ͧ

    View full-size slide

  4. mockito-kotlin
    • KotlinͰMockitoΛ࢖͏ͨΊͷϥΠϒϥϦ
    • https://github.com/nhaarman/mockito-kotlin
    • ˒800 over

    View full-size slide

  5. Ͳ͕͍͍͔͜
    • ϞοΫ͕ݟ΍͘͢ͳΔ
    • Null҆શରԠ
    • άϩʔόϧؔ਺ͷ௥Ճ
    • when໰୊ରԠ

    View full-size slide

  6. Mockito in Java
    // Java
    import static org.mockito.Mockito.*;
    Hoge mock = mock(Hoge.class);
    when(mock.sayHello()).thenReturn("Hello");
    when(mock.sayBye()).thenReturn("Bye");

    View full-size slide

  7. Convert to Kotlin
    // Kotlin
    import org.mockito.Mockito.*
    val mock = mock(Hoge::class.java)
    `when`(mock.sayHello()).thenReturn("Hello")
    `when`(mock.sayBye()).thenReturn("Bye")

    View full-size slide

  8. mockito-kotlin
    // Kotlin
    import com.nhaarman.mockito_kotlin.*
    val mock = mock {
    on { sayHello() } doReturn "Hello"
    on { sayBye() } doReturn "Bye"
    }

    View full-size slide

  9. Null҆શରԠ
    • Null҆શͷ͍ͤͰɺMockitoͷanyͰNPE
    • NonNullͳҾ਺ʹanyΛ౉͢ͱൃੜ
    • anyͱanyOrNull

    View full-size slide

  10. άϩʔόϧؔ਺
    • any, anyOrNull
    • atLeast, only, never
    • verify
    • whenever

    View full-size slide

  11. when໰୊
    • Mockitoͷwhenϝιου
    • Kotlinͷwhen͸༧໿ޠ
    • `when`ͱॻ͔͘͠ແ͍
    • mockito-kotlinͰ͸୅ΘΓʹwhenever΍on͕
    ࢖͑Δ

    View full-size slide

  12. mockito-kotlin͸͍͍ͧ

    View full-size slide