-
Notifications
You must be signed in to change notification settings - Fork 202
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #239 from nhaarman/coroutines
Support coroutines
- Loading branch information
Showing
6 changed files
with
198 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,3 +18,5 @@ out/ | |
.idea/uiDesigner.xml | ||
.idea/vcs.xml | ||
.idea/workspace.xml | ||
|
||
*.orig |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
@file:Suppress("EXPERIMENTAL_FEATURE_WARNING") | ||
|
||
package test | ||
|
||
import com.nhaarman.expect.expect | ||
import com.nhaarman.mockitokotlin2.doReturn | ||
import com.nhaarman.mockitokotlin2.mock | ||
import com.nhaarman.mockitokotlin2.verify | ||
import com.nhaarman.mockitokotlin2.verifyBlocking | ||
import kotlinx.coroutines.experimental.CommonPool | ||
import kotlinx.coroutines.experimental.delay | ||
import kotlinx.coroutines.experimental.runBlocking | ||
import kotlinx.coroutines.experimental.withContext | ||
import org.junit.Test | ||
|
||
|
||
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() } | ||
} | ||
} | ||
|
||
interface SomeInterface { | ||
|
||
suspend fun suspending(): Int | ||
fun nonsuspending(): Int | ||
} | ||
|
||
class SomeClass { | ||
|
||
suspend fun result(r: Int) = withContext(CommonPool) { r } | ||
|
||
suspend fun delaying() = withContext(CommonPool) { | ||
delay(100) | ||
42 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
include 'mockito-kotlin' | ||
include 'tests' | ||
include 'tests' |