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

KotlinConf Report @ca.kt#7

TakuSemba
October 15, 2018

KotlinConf Report @ca.kt#7

TakuSemba

October 15, 2018
Tweet

More Decks by TakuSemba

Other Decks in Programming

Transcript

  1. View Slide

  2. @takusemba
    https://github.com/TakuSemba

    View Slide

  3. View Slide

  4. View Slide

  5. View Slide

  6. data class Heineken (
    val price: String,
    val volume: String,
    val createdAt: Long
    )

    View Slide

  7. class HeinekenFactory {
    fun brew(): Heineken {
    //…
    }
    }
    data class Heineken (
    val price: String,
    val volume: String,
    val createdAt: Long
    )

    View Slide

  8. class HeinekenFactory {
    fun brew(): Heineken {
    //…
    }
    }
    private val mockFactory = mock()
    data class Heineken (
    val price: String,
    val volume: String,
    val createdAt: Long
    )

    View Slide

  9. class HeinekenFactory {
    fun brew(): Heineken {
    //…
    }
    }
    private val mockFactory = mock()
    data class Heineken (
    val price: String,
    val volume: String,
    val createdAt: Long
    )

    View Slide

  10. class HeinekenFactory {
    fun brew(): Heineken {
    //…
    }
    }
    private val mockFactory = mock()
    class HeinekenFactory
    data class Heineken (
    val price: String,
    val volume: String,
    val createdAt: Long
    )
    private val mockFactory = mock()

    View Slide

  11. class HeinekenFactory {
    fun brew(): Heineken {
    //…
    }
    }
    private val mockFactory = mock()
    class HeinekenFactory // final class
    data class Heineken (
    val price: String,
    val volume: String,
    val createdAt: Long
    )
    private val mockFactory = mock()

    View Slide

  12. open class HeinekenFactory {
    fun brew(): Heineken {
    //…
    }
    }
    private val mockFactory = mock()
    data class Heineken (
    val price: String,
    val volume: String,
    val createdAt: Long
    )

    View Slide

  13. class HeinekenFactory: BeerFactory {
    override fun brew(): Heineken {
    //…
    }
    }
    private val mockFactory = mock()
    data class Heineken (
    val price: String,
    val volume: String,
    val createdAt: Long
    )

    View Slide

  14. MockK

    View Slide

  15. data class Heineken (
    val price: String,
    val volume: String,
    val createdAt: Long
    )
    class HeinekenFactory {
    fun brew(): Heineken {
    //…
    }
    }

    View Slide

  16. data class Heineken (
    val price: String,
    val volume: String,
    val createdAt: Long
    )
    class HeinekenFactory {
    fun brew(): Heineken {
    //…
    }
    }
    private val mockFactory = mockk()

    View Slide

  17. private val mockFactory = mockk()

    View Slide

  18. @MockK
    private lateinit var mockFactory: HeinekenFactory
    init {
    MockKAnnotations.init(this)
    }
    every { mockFactory.brew() } returns heineken

    View Slide

  19. @MockK(relaxed = true)
    private lateinit var mockFactory: HeinekenFactory
    init {
    MockKAnnotations.init(this)
    }
    every { mockFactory.brew() } returns heineken

    View Slide

  20. @MockK(relaxed = true, relaxUnitFun = true)
    private lateinit var mockFactory: HeinekenFactory
    init {
    MockKAnnotations.init(this)
    }
    every { mockFactory.brew() } returns heineken

    View Slide

  21. @MockK
    private lateinit var mockFactory: HeinekenFactory
    init {
    MockKAnnotations.init(this)
    }
    @Test
    fun testFactory() {
    mockFactory.brew()
    verify { mockFactory.brew() }
    }

    View Slide

  22. @MockK
    private lateinit var mockFactory: HeinekenFactory
    init {
    MockKAnnotations.init(this)
    }
    @Test
    fun testFactory() {
    mockFactory.brew()
    verify(exactly = 1) { mockFactory.brew() }
    }

    View Slide

  23. @MockK
    private lateinit var mockFactory: HeinekenFactory
    init {
    MockKAnnotations.init(this)
    }
    @Test
    fun testFactory() {
    mockFactory.brew()
    verify(inverse = true) { mockFactory.brew() }
    }

    View Slide

  24. @MockK
    private lateinit var mockFactory: HeinekenFactory
    init {
    MockKAnnotations.init(this)
    }
    @Test
    fun testFactory() {
    mockFactory.prepare()
    mockFactory.brew()
    mockFactory.deliver()
    verifyOrder {
    mockFactory.prepare()
    mockFactory.deliver()
    }
    }

    View Slide

  25. @MockK
    private lateinit var mockFactory: HeinekenFactory
    init {
    MockKAnnotations.init(this)
    }
    @Test
    fun testFactory() {
    mockFactory.prepare()
    mockFactory.brew()
    mockFactory.deliver()
    verifySequence {
    mockFactory.prepare()
    mockFactory.brew()
    mockFactory.deliver()
    }
    }

    View Slide

  26. JUnit5

    View Slide

  27. class HeinekenFactoryTest {
    init {
    println("init")
    }
    @Test
    fun testFactory1() {
    println("testFactory1")
    }
    @Test
    fun testFactory2() {
    println("testFactory2")
    }
    }

    View Slide

  28. class HeinekenFactoryTest {
    init {
    println("init")
    }
    @Test
    fun testFactory1() {
    println("testFactory1")
    }
    @Test
    fun testFactory2() {
    println("testFactory2")
    }
    }

    View Slide

  29. class HeinekenFactoryTest {
    init {
    println("init")
    }
    @Test
    fun testFactory1() {
    println("testFactory1")
    }
    @Test
    fun testFactory2() {
    println("testFactory2")
    }
    }

    View Slide

  30. @TestInstance(TestInstance.Lifecycle.PER_CLASS)
    class HeinekenFactoryTest {
    init {
    println("init")
    }
    @Test
    fun testFactory1() {
    println("testFactory1")
    }
    @Test
    fun testFactory2() {
    println("testFactory2")
    }
    }

    View Slide

  31. @TestInstance(TestInstance.Lifecycle.PER_CLASS)
    class HeinekenFactoryTest {
    init {
    println("init")
    }
    @Test
    fun testFactory1() {
    println("testFactory1")
    }
    @Test
    fun testFactory2() {
    println("testFactory2")
    }
    }

    View Slide

  32. @TestInstance(TestInstance.Lifecycle.PER_CLASS)
    class HeinekenFactoryTest {
    init {
    println("init")
    }
    @Test
    fun testFactory1() {
    println("testFactory1")
    }
    @Test
    fun testFactory2() {
    println("testFactory2")
    }
    }

    View Slide

  33. @TestInstance(TestInstance.Lifecycle.PER_CLASS)
    class HeinekenFactoryTest {
    @MockK
    private lateinit var mockFactory: HeinekenFactory
    init {
    println(“init")
    }
    @Test
    fun testFactory1() {
    println("testFactory1")
    }
    @Test
    fun testFactory2() {
    println("testFactory2")
    }
    }

    View Slide

  34. @TestInstance(TestInstance.Lifecycle.PER_CLASS)
    class HeinekenFactoryTest {
    @MockK
    private lateinit var mockFactory: HeinekenFactory
    init {
    MockKAnnotations.init(this)
    }
    @Test
    fun testFactory1() {
    println("testFactory1")
    }
    @Test
    fun testFactory2() {
    println("testFactory2")
    }
    }

    View Slide

  35. @TestInstance(TestInstance.Lifecycle.PER_CLASS)
    class HeinekenFactoryTest {
    @MockK
    private lateinit var mockFactory: HeinekenFactory
    init {
    MockKAnnotations.init(this)
    }
    @BeforeEach
    fun beforeEach() {
    clearMocks(mockFactory)
    }
    @Test
    fun testFactory1() {
    //…
    }
    @Test
    fun testFactory2() {
    //…
    }
    }

    View Slide

  36. class HeinekenFactoryTest {
    @MockK
    private lateinit var mockFactory: HeinekenFactory
    init {
    MockKAnnotations.init(this)
    }
    @Nested
    inner class Factory1 {
    @Test
    fun test1() {
    //…
    }
    @Test
    fun test2() {
    //…
    }
    }
    //…
    }

    View Slide

  37. class HeinekenFactoryTest {
    @MockK
    private lateinit var mockFactory: HeinekenFactory
    init {
    MockKAnnotations.init(this)
    }
    @Nested
    inner class Factory1 {
    @Test
    fun test1() {
    //…
    }
    @Test
    fun test2() {
    //…
    }
    }
    //…
    }

    View Slide

  38. class HeinekenFactoryTest {
    @MockK
    private lateinit var mockFactory: HeinekenFactory
    init {
    MockKAnnotations.init(this)
    }
    @Nested
    inner class Factory1 {
    @Test
    fun checkHeinekenFactoryDoSomeWork() {
    //…
    }
    @Test
    fun checkHeinekenFactoryDoTheOtherWork() {
    //…
    }
    }
    //…
    }

    View Slide

  39. class HeinekenFactoryTest {
    @MockK
    private lateinit var mockFactory: HeinekenFactory
    init {
    MockKAnnotations.init(this)
    }
    @Nested
    inner class Factory1 {
    @Test
    fun `check HeinekenFactory do some work`() {
    //…
    }
    @Test
    fun `check HeinekenFactory do the other work`() {
    //…
    }
    }
    //…
    }

    View Slide

  40. class HeinekenFactoryTest {
    @MockK
    private lateinit var mockFactory: HeinekenFactory
    init {
    MockKAnnotations.init(this)
    }
    @Nested
    inner class Factory1 {
    @Test
    fun `check HeinekenFactory do some work`() {
    //…
    }
    @Test
    fun `check HeinekenFactory do the other work`() {
    //…
    }
    }
    //…
    }

    View Slide

  41. Data Class

    View Slide

  42. @Test
    fun testHeineken() {
    val actual = Heineken(
    price = "2$",
    volume = "350ml",
    createdAt = 1539522908
    )
    }

    View Slide

  43. @Test
    fun testHeineken() {
    val actual = Heineken(
    price = "2$",
    volume = "350ml",
    createdAt = 1539522908
    )
    assertThat(actual.price).isEqualTo("3$")
    assertThat(actual.volume).isEqualTo("350ml$")
    assertThat(actual.createdAt).isEqualTo(1539522908)
    }

    View Slide

  44. @Test
    fun testHeineken() {
    val actual = Heineken(
    price = "2$",
    volume = "350ml",
    createdAt = 1539522908
    )
    assertThat(actual.price).isEqualTo("3$")
    assertThat(actual.volume).isEqualTo("350ml$")
    assertThat(actual.createdAt).isEqualTo(1539522908)
    }

    View Slide

  45. @Test
    fun testHeineken() {
    val actual = Heineken(
    price = "2$",
    volume = "350ml",
    createdAt = 1539522908
    )
    val expected = Heineken(
    price = "3$",
    volume = "350ml",
    createdAt = 1539522908
    )
    assertThat(actual).isEqualTo(expected)
    }

    View Slide

  46. @Test
    fun testHeineken() {
    val actual = Heineken(
    price = "2$",
    volume = "350ml",
    createdAt = 1539522908
    )
    val expected = Heineken(
    price = "3$",
    volume = "350ml",
    createdAt = 1539522908
    )
    assertThat(actual).isEqualTo(expected)
    }

    View Slide

  47. @Test
    fun testHeineken() {
    val actual = Heineken(
    price = "2$",
    volume = "350ml",
    createdAt = 1539522908
    )
    val expected = Heineken(
    price = "3$",
    volume = "350ml",
    createdAt = 1539522908
    )
    assertThat(actual)
    .isEqualToIgnoringGivenFields(expected, "price")
    }

    View Slide

  48. @Test
    fun testHeineken() {
    val actual = Heineken(
    price = "2$",
    volume = "350ml",
    createdAt = 1539522908
    )
    val expected = Heineken(
    price = "3$",
    volume = "350ml",
    createdAt = 1539522908
    )
    assertThat(actual)
    .isEqualToIgnoringGivenFields(expected, “price”)
    assertThat(actual)
    .isEqualToComparingOnlyGivenFields(expected, "price")
    }

    View Slide

  49. fun createHeineken(
    price: String = "2$",
    volume: String = "300ml",
    createdAt: Long = System.currentTimeMillis()
    ) = Heineken(
    price = price,
    volume = volume,
    createdAt = createdAt
    )

    View Slide

  50. fun createHeineken(
    price: String = "2$",
    volume: String = "300ml",
    createdAt: Long = System.currentTimeMillis()
    ) = Heineken(
    price = price,
    volume = volume,
    createdAt = createdAt
    )
    @Test
    fun testHeineken() {
    val heineken1 = createHeineken()
    val heineken2 = createHeineken(price = "3$")
    val heineken3 = createHeineken(price = "3$", volume = “500ml")
    //…
    }

    View Slide

  51. View Slide