diff options
Diffstat (limited to 'mockito-kotlin/src/test/kotlin/test/CoroutinesTest.kt')
-rw-r--r-- | mockito-kotlin/src/test/kotlin/test/CoroutinesTest.kt | 176 |
1 files changed, 176 insertions, 0 deletions
diff --git a/mockito-kotlin/src/test/kotlin/test/CoroutinesTest.kt b/mockito-kotlin/src/test/kotlin/test/CoroutinesTest.kt new file mode 100644 index 0000000..5ca6eb6 --- /dev/null +++ b/mockito-kotlin/src/test/kotlin/test/CoroutinesTest.kt @@ -0,0 +1,176 @@ +@file:Suppress("EXPERIMENTAL_FEATURE_WARNING") + +package test + +import com.nhaarman.expect.expect +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.delay +import kotlinx.coroutines.runBlocking +import kotlinx.coroutines.withContext +import org.junit.Test +import org.mockito.kotlin.* + + +class CoroutinesTest { + + @Test + fun stubbingSuspending() { + /* Given */ + val m = mock<SomeInterface> { + onBlocking { suspending() } doReturn 42 + } + + /* When */ + val result = runBlocking { m.suspending() } + + /* Then */ + expect(result).toBe(42) + } + + @Test + fun stubbingSuspending_usingSuspendingFunction() { + /* Given */ + val m = mock<SomeInterface> { + onBlocking { suspending() } doReturn runBlocking { SomeClass().result(42) } + } + + /* When */ + val result = runBlocking { m.suspending() } + + /* Then */ + expect(result).toBe(42) + } + + @Test + fun stubbingSuspending_runBlocking() = runBlocking { + /* Given */ + val m = mock<SomeInterface> { + onBlocking { suspending() } doReturn 42 + } + + /* When */ + val result = m.suspending() + + /* Then */ + expect(result).toBe(42) + } + + @Test + fun stubbingNonSuspending() { + /* Given */ + val m = mock<SomeInterface> { + onBlocking { nonsuspending() } doReturn 42 + } + + /* When */ + val result = m.nonsuspending() + + /* Then */ + expect(result).toBe(42) + } + + @Test + fun stubbingNonSuspending_runBlocking() = runBlocking { + /* Given */ + val m = mock<SomeInterface> { + onBlocking { nonsuspending() } doReturn 42 + } + + /* When */ + val result = m.nonsuspending() + + /* Then */ + expect(result).toBe(42) + } + + @Test + fun delayingResult() { + /* Given */ + val m = SomeClass() + + /* When */ + val result = runBlocking { m.delaying() } + + /* Then */ + expect(result).toBe(42) + } + + @Test + fun delayingResult_runBlocking() = runBlocking { + /* Given */ + val m = SomeClass() + + /* When */ + val result = m.delaying() + + /* Then */ + expect(result).toBe(42) + } + + @Test + fun verifySuspendFunctionCalled() { + /* Given */ + val m = mock<SomeInterface>() + + /* When */ + runBlocking { m.suspending() } + + /* Then */ + runBlocking { verify(m).suspending() } + } + + @Test + fun verifySuspendFunctionCalled_runBlocking() = runBlocking<Unit> { + val m = mock<SomeInterface>() + + m.suspending() + + verify(m).suspending() + } + + @Test + fun verifySuspendFunctionCalled_verifyBlocking() { + val m = mock<SomeInterface>() + + runBlocking { m.suspending() } + + verifyBlocking(m) { suspending() } + } + + @Test + fun verifyAtLeastOnceSuspendFunctionCalled_verifyBlocking() { + val m = mock<SomeInterface>() + + runBlocking { m.suspending() } + runBlocking { m.suspending() } + + verifyBlocking(m, atLeastOnce()) { suspending() } + } + + @Test + fun verifySuspendMethod() = runBlocking { + val testSubject: SomeInterface = mock() + + testSubject.suspending() + + inOrder(testSubject) { + verify(testSubject).suspending() + } + } +} + +interface SomeInterface { + + suspend fun suspending(): Int + fun nonsuspending(): Int +} + +class SomeClass { + + suspend fun result(r: Int) = withContext(Dispatchers.Default) { r } + + suspend fun delaying() = withContext(Dispatchers.Default) { + delay(100) + 42 + } +} |