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

Mocking Kotlin with MockK

Mocking Kotlin with MockK

Hadi Tok

March 23, 2019
Tweet

More Decks by Hadi Tok

Other Decks in Programming

Transcript

  1. UNIT TESTING NEDIR? ➤ Unit: En küçük test edilebilir birim

    (Class, Function) ➤ Birimler diğer birimlerden bağımısız olarak test edilir. ➤ Diğer birimler Mock veya Fake ile değiştirilir.
  2. MOCK ➤ Anlamı: Taklit, Alay ➤ Unit testlerde test ettiğimiz

    birimin ihtiyacı olan objeleri objeleri taklit ederek, bu objeler üzerinde tam kontrol sağlar
  3. NELERI KONTROL EDIYORUZ? ➤ Değişken ve fonksiyonların döneceği değerleri ➤

    Fonksiyonların doğru parametreler ile çağırılıp çağrılmadığını
  4. data class User(val userId: String, val userName: String) interface UserApi

    { fun deleteUser(userId: String): Boolean } class UserManager(private val userApi: UserApi) { fun deleteUser(user: User): Boolean { return userApi.deleteUser(user.userId) } }
  5. @Test fun basicTest() { val userApi: UserApi = mockk() val

    userManager = UserManager(userApi) }
  6. @Test fun basicTest() { val userApi: UserApi = mockk() val

    userManager = UserManager(userApi) val userId = "123" val user: User = mockk() every { user.userId } returns userId }
  7. @Test fun basicTest() { val userApi: UserApi = mockk() val

    userManager = UserManager(userApi) val userId = "123" val user: User = mockk() every { user.userId } returns userId every { userApi.deleteUser(userId) } returns true }
  8. @Test fun basicTest() { val userApi: UserApi = mockk() val

    userManager = UserManager(userApi) val userId = "123" val user: User = mockk() every { user.userId } returns userId every { userApi.deleteUser(userId) } returns true val result = userManager.deleteUser(user) }
  9. @Test fun basicTest() { val userApi: UserApi = mockk() val

    userManager = UserManager(userApi) val userId = "123" val user: User = mockk() every { user.userId } returns userId every { userApi.deleteUser(userId) } returns true val result = userManager.deleteUser(user) assert(result).toBe(true) }
  10. @Test fun basicTest() { val userApi: UserApi = mockk() val

    userManager = UserManager(userApi) val userId = "123" val user: User = mockk() every { user.userId } returns userId every { userApi.deleteUser(userId) } returns true val result = userManager.deleteUser(user) assert(result).toBe(true) verify { userApi.deleteUser(userId) } }
  11. @Test fun basicTest() { val userApi: UserApi = mockk() val

    userManager = UserManager(userApi) val userId = "123" val user: User = mockk() every { user.userId } returns userId every { userApi.deleteUser(userId) } returns true val result = userManager.deleteUser(user) assert(result).toBe(true) verify { userApi.deleteUser(match { it.startsWith("12") }) } }
  12. @Test fun basicTest() { val userApi: UserApi = mockk() val

    userManager = UserManager(userApi) val userId = "123" val user: User = mockk() every { user.userId } returns userId every { userApi.deleteUser(userId) } returns true val result = userManager.deleteUser(user) assert(result).toBe(true) verify { userApi.deleteUser(any()) } }
  13. @MockK lateinit var userApi: UserApi @MockK lateinit var user: User

    val userId = “123" lateinit var userManager:UserManager @Before fun initTests() { MockKAnnotations.init(this) userManager = UserManager(userApi) every { user.userId } returns userId } @Test fun basicTest() { every { userApi.deleteUser(userId) } returns true val result = userManager.deleteUser(user) assert(result).toBe(true) verify { userApi.deleteUser(userId) } }
  14. object Calculator { fun add(first: Int, second: Int) = first

    + second } @Test fun objectMock() { val result = Calculator.add(1, 2) assert(result).toBe(3) }
  15. object Calculator { fun add(first: Int, second: Int) = first

    + second } @Test fun objectMock() { val result = Calculator.add(1, 2) assert(result).toBe(3) mockkObject(Calculator) }
  16. object Calculator { fun add(first: Int, second: Int) = first

    + second } @Test fun objectMock() { val result = Calculator.add(1, 2) assert(result).toBe(3) mockkObject(Calculator) every { Calculator.add(1, 2) } returns 42 }
  17. object Calculator { fun add(first: Int, second: Int) = first

    + second } @Test fun objectMock() { val result = Calculator.add(1, 2) assert(result).toBe(3) mockkObject(Calculator) every { Calculator.add(1, 2) } returns 41 val mockedResult = Calculator.add(1, 2) }
  18. object Calculator { fun add(first: Int, second: Int) = first

    + second } @Test fun objectMock() { val result = Calculator.add(1, 2) assert(result).toBe(3) mockkObject(Calculator) every { Calculator.add(1, 2) } returns 41 val mockedResult = Calculator.add(1, 2) assert(mockedResult).toBe(42) }
  19. @Test fun staticMock() { val result = CalculatorStatic.add(1, 2) assert(result).toBe(3)

    mockkStatic(CalculatorStatic ::class) every { CalculatorStatic.add(1, 2) } returns 42 }
  20. @Test fun staticMock() { val result = CalculatorStatic.add(1, 2) assert(result).toBe(3)

    mockkStatic(CalculatorStatic ::class) every { CalculatorStatic.add(1, 2) } returns 41 val mockedResult = CalculatorStatic.add(1, 2) }
  21. @Test fun staticMock() { val result = CalculatorStatic.add(1, 2) assert(result).toBe(3)

    mockkStatic(CalculatorStatic ::class) every { CalculatorStatic.add(1, 2) } returns 41 val mockedResult = CalculatorStatic.add(1, 2) assert(mockedResult).toBe(42) }
  22. data class User(val userId: String, val userName: String) interface UserApi

    { suspend fun deleteUser(userId: String): Boolean }
  23. data class User(val userId: String, val userName: String) interface UserApi

    { suspend fun deleteUser(userId: String): Boolean } class UserManager(private val userApi: UserApi) { suspend fun deleteUser(user: User): Boolean { return userApi.deleteUser(user.userId) } }
  24. @Test fun basicTest() = runBlocking { val userApi: UserApi =

    mockk() val userManager = UserManager(userApi) val userId = "123" val user: User = mockk() every { user.userId } returns userId }
  25. @Test fun basicTest() = runBlocking { val userApi: UserApi =

    mockk() val userManager = UserManager(userApi) val userId = "123" val user: User = mockk() every { user.userId } returns userId coEvery { userApi.deleteUser(userId) } returns true }
  26. @Test fun basicTest() = runBlocking { val userApi: UserApi =

    mockk() val userManager = UserManager(userApi) val userId = “123" val user: User = mockk() every { user.userId } returns userId coEvery { userApi.deleteUser(userId) } returns true val result = userManager.deleteUser(user) }
  27. @Test fun basicTest() = runBlocking { val userApi: UserApi =

    mockk() val userManager = UserManager(userApi) val userId = “123" val user: User = mockk() every { user.userId } returns userId coEvery { userApi.deleteUser(userId) } returns true val result = userManager.deleteUser(user) assert(result).toBe(true) coVerify { userApi.deleteUser(userId) } }