KotlinConf Report @ca.kt#7

A374f41eab3f73c50d8bab0652cb207a?s=47 TakuSemba
October 15, 2018

KotlinConf Report @ca.kt#7

A374f41eab3f73c50d8bab0652cb207a?s=128

TakuSemba

October 15, 2018
Tweet

Transcript

  1. 1.
  2. 3.
  3. 4.
  4. 5.
  5. 7.

    class HeinekenFactory { fun brew(): Heineken { //… } }

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

    class HeinekenFactory { fun brew(): Heineken { //… } }

    private val mockFactory = mock<HeinekenFactory>() data class Heineken ( val price: String, val volume: String, val createdAt: Long )
  7. 9.

    class HeinekenFactory { fun brew(): Heineken { //… } }

    private val mockFactory = mock<HeinekenFactory>() data class Heineken ( val price: String, val volume: String, val createdAt: Long )
  8. 10.

    class HeinekenFactory { fun brew(): Heineken { //… } }

    private val mockFactory = mock<HeinekenFactory>() class HeinekenFactory data class Heineken ( val price: String, val volume: String, val createdAt: Long ) private val mockFactory = mock<HeinekenFactory>()
  9. 11.

    class HeinekenFactory { fun brew(): Heineken { //… } }

    private val mockFactory = mock<HeinekenFactory>() class HeinekenFactory // final class data class Heineken ( val price: String, val volume: String, val createdAt: Long ) private val mockFactory = mock<HeinekenFactory>()
  10. 12.

    open class HeinekenFactory { fun brew(): Heineken { //… }

    } private val mockFactory = mock<HeinekenFactory>() data class Heineken ( val price: String, val volume: String, val createdAt: Long )
  11. 13.

    class HeinekenFactory: BeerFactory { override fun brew(): Heineken { //…

    } } private val mockFactory = mock<BeerFactory>() data class Heineken ( val price: String, val volume: String, val createdAt: Long )
  12. 14.
  13. 15.

    data class Heineken ( val price: String, val volume: String,

    val createdAt: Long ) class HeinekenFactory { fun brew(): Heineken { //… } }
  14. 16.

    data class Heineken ( val price: String, val volume: String,

    val createdAt: Long ) class HeinekenFactory { fun brew(): Heineken { //… } } private val mockFactory = mockk<HeinekenFactory>()
  15. 19.

    @MockK(relaxed = true) private lateinit var mockFactory: HeinekenFactory init {

    MockKAnnotations.init(this) } every { mockFactory.brew() } returns heineken
  16. 20.

    @MockK(relaxed = true, relaxUnitFun = true) private lateinit var mockFactory:

    HeinekenFactory init { MockKAnnotations.init(this) } every { mockFactory.brew() } returns heineken
  17. 21.

    @MockK private lateinit var mockFactory: HeinekenFactory init { MockKAnnotations.init(this) }

    @Test fun testFactory() { mockFactory.brew() verify { mockFactory.brew() } }
  18. 22.

    @MockK private lateinit var mockFactory: HeinekenFactory init { MockKAnnotations.init(this) }

    @Test fun testFactory() { mockFactory.brew() verify(exactly = 1) { mockFactory.brew() } }
  19. 23.

    @MockK private lateinit var mockFactory: HeinekenFactory init { MockKAnnotations.init(this) }

    @Test fun testFactory() { mockFactory.brew() verify(inverse = true) { mockFactory.brew() } }
  20. 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() } }
  21. 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() } }
  22. 26.
  23. 27.

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

    { println("testFactory1") } @Test fun testFactory2() { println("testFactory2") } }
  24. 28.

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

    { println("testFactory1") } @Test fun testFactory2() { println("testFactory2") } }
  25. 29.

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

    { println("testFactory1") } @Test fun testFactory2() { println("testFactory2") } }
  26. 30.

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

    testFactory1() { println("testFactory1") } @Test fun testFactory2() { println("testFactory2") } }
  27. 31.

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

    testFactory1() { println("testFactory1") } @Test fun testFactory2() { println("testFactory2") } }
  28. 32.

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

    testFactory1() { println("testFactory1") } @Test fun testFactory2() { println("testFactory2") } }
  29. 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") } }
  30. 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") } }
  31. 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() { //… } }
  32. 36.

    class HeinekenFactoryTest { @MockK private lateinit var mockFactory: HeinekenFactory init

    { MockKAnnotations.init(this) } @Nested inner class Factory1 { @Test fun test1() { //… } @Test fun test2() { //… } } //… }
  33. 37.

    class HeinekenFactoryTest { @MockK private lateinit var mockFactory: HeinekenFactory init

    { MockKAnnotations.init(this) } @Nested inner class Factory1 { @Test fun test1() { //… } @Test fun test2() { //… } } //… }
  34. 38.

    class HeinekenFactoryTest { @MockK private lateinit var mockFactory: HeinekenFactory init

    { MockKAnnotations.init(this) } @Nested inner class Factory1 { @Test fun checkHeinekenFactoryDoSomeWork() { //… } @Test fun checkHeinekenFactoryDoTheOtherWork() { //… } } //… }
  35. 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`() { //… } } //… }
  36. 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`() { //… } } //… }
  37. 42.

    @Test fun testHeineken() { val actual = Heineken( price =

    "2$", volume = "350ml", createdAt = 1539522908 ) }
  38. 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) }
  39. 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) }
  40. 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) }
  41. 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) }
  42. 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") }
  43. 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") }
  44. 49.

    fun createHeineken( price: String = "2$", volume: String = "300ml",

    createdAt: Long = System.currentTimeMillis() ) = Heineken( price = price, volume = volume, createdAt = createdAt )
  45. 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") //… }
  46. 51.