Pro Yearly is on sale from $80 to $50! »

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. None
  2. @takusemba https://github.com/TakuSemba

  3. None
  4. None
  5. None
  6. data class Heineken ( val price: String, val volume: String,

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

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

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

    private val mockFactory = mock<HeinekenFactory>() data class Heineken ( val price: String, val volume: String, val createdAt: Long )
  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>()
  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>()
  12. open class HeinekenFactory { fun brew(): Heineken { //… }

    } private val mockFactory = mock<HeinekenFactory>() data class Heineken ( val price: String, val volume: String, val createdAt: Long )
  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 )
  14. MockK

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    testFactory1() { println("testFactory1") } @Test fun testFactory2() { println("testFactory2") } }
  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") } }
  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") } }
  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() { //… } }
  36. class HeinekenFactoryTest { @MockK private lateinit var mockFactory: HeinekenFactory init

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

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

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

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

    "2$", volume = "350ml", createdAt = 1539522908 ) }
  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) }
  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) }
  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) }
  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) }
  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") }
  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") }
  49. fun createHeineken( price: String = "2$", volume: String = "300ml",

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