Skip to content

Commit

Permalink
Replace mocked Task with stubbed one
Browse files Browse the repository at this point in the history
  • Loading branch information
janseeger committed Dec 20, 2023
1 parent b56a5b2 commit 73e638f
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 13 deletions.
1 change: 0 additions & 1 deletion dachlatten-google/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,4 @@ dependencies {
compileOnly(libs.coroutines.playServices)

testImplementation(libs.coroutines.playServices)
testImplementation(libs.bundles.mockk)
}
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
package de.sipgate.dachlatten.google

import android.app.Activity
import com.google.android.gms.tasks.OnFailureListener
import com.google.android.gms.tasks.OnSuccessListener
import com.google.android.gms.tasks.Task
import io.mockk.coEvery
import io.mockk.mockk
import kotlinx.coroutines.tasks.await
import kotlinx.coroutines.test.runTest
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertFalse
import org.junit.jupiter.api.Assertions.assertInstanceOf
import org.junit.jupiter.api.Assertions.assertNull
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Disabled
import org.junit.jupiter.api.Test
import java.util.concurrent.Executor

class TaskAwaitResultTest {
@Test
@Disabled
// @Disabled
fun resultHandlesSuccessCorrectly() = runTest {
val successfulTask = mockk<Task<String>> {
coEvery { await() } returns "success"
}
val successfulTask = DummyResult(success = "success")

val result = successfulTask.awaitResult()
assertTrue(result.isSuccess)
Expand All @@ -29,16 +27,55 @@ class TaskAwaitResultTest {
}

@Test
@Disabled
fun resultHandlesErrorCorrectly() = runTest {
val failingTask = mockk<Task<String>> {
coEvery { await() } throws Exception("you shall not pass!")
}
val failingTask = DummyResult<String>(error = Exception("you shall not pass!"))

val result = failingTask.awaitResult()
assertFalse(result.isSuccess)
assertNull(result.getOrNull())
assertTrue(result.isFailure)
assertInstanceOf(Exception::class.java, result.exceptionOrNull())
}

private class DummyResult<T>(
private val success: T? = null,
private val error: Throwable? = null
) : Task<T>() {
override fun isComplete(): Boolean = true

override fun isSuccessful(): Boolean = success != null

override fun isCanceled(): Boolean = false

override fun getResult(): T? = success

override fun getException(): java.lang.Exception? = error as? java.lang.Exception
override fun addOnSuccessListener(p0: Activity, p1: OnSuccessListener<in T>): Task<T> {
TODO("Not yet implemented")
}

override fun addOnSuccessListener(p0: Executor, p1: OnSuccessListener<in T>): Task<T> {
TODO("Not yet implemented")
}

override fun addOnSuccessListener(p0: OnSuccessListener<in T>): Task<T> {
TODO("Not yet implemented")
}

override fun <X : Throwable?> getResult(p0: Class<X>): T? {
TODO("Not yet implemented")
}

override fun addOnFailureListener(p0: OnFailureListener): Task<T> {
TODO("Not yet implemented")
}

override fun addOnFailureListener(p0: Executor, p1: OnFailureListener): Task<T> {
TODO("Not yet implemented")
}

override fun addOnFailureListener(p0: Activity, p1: OnFailureListener): Task<T> {
TODO("Not yet implemented")
}
}
}

0 comments on commit 73e638f

Please sign in to comment.