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. class HeinekenFactory { fun brew(): Heineken { //… } }

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

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

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

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

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

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

    val createdAt: Long ) class HeinekenFactory { fun brew(): Heineken { //… } } private val mockFactory = mockk<HeinekenFactory>()
  10. @MockK(relaxed = true) private lateinit var mockFactory: HeinekenFactory init {

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

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

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

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

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

    @Test fun testFactory() { mockFactory.prepare() mockFactory.brew() mockFactory.deliver() verifyOrder { mockFactory.prepare() mockFactory.deliver() } }
  16. @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() } }
  17. class HeinekenFactoryTest { init { println("init") } @Test fun testFactory1()

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

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

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

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

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

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

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

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

    { MockKAnnotations.init(this) } @Nested inner class Factory1 { @Test fun checkHeinekenFactoryDoSomeWork() { //… } @Test fun checkHeinekenFactoryDoTheOtherWork() { //… } } //… }
  29. 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`() { //… } } //… }
  30. 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`() { //… } } //… }
  31. @Test fun testHeineken() { val actual = Heineken( price =

    "2$", volume = "350ml", createdAt = 1539522908 ) }
  32. @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) }
  33. @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) }
  34. @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) }
  35. @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) }
  36. @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") }
  37. @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") }
  38. fun createHeineken( price: String = "2$", volume: String = "300ml",

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