Skip to content

Commit 176fb3a

Browse files
authored
Improve test coverage for coroutines (#526)
1 parent 391bb3f commit 176fb3a

File tree

4 files changed

+54
-0
lines changed

4 files changed

+54
-0
lines changed

tests/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ dependencies {
1919

2020
testImplementation 'junit:junit:4.13.2'
2121
testImplementation "com.nhaarman:expect.kt:1.0.1"
22+
testImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:1.9.0-RC"
2223
}
2324

2425
tasks.withType(KotlinCompile).configureEach {

tests/src/test/kotlin/test/Classes.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ class Closed
4545
interface Methods {
4646

4747
fun closed(c: Closed)
48+
fun classClosed(c: Class<Closed>)
49+
suspend fun coroutinesClosed(c: Closed)
4850
fun closedArray(a: Array<Closed>)
4951
fun closedNullableArray(a: Array<Closed?>)
5052
fun closedCollection(c: Collection<Closed>)
@@ -77,6 +79,8 @@ interface Methods {
7779
fun nullableStringResult(): String?
7880
fun builderMethod(): Methods
7981
fun varargBooleanResult(vararg values: String): Boolean
82+
suspend fun coroutinesClosedBooleanResult(c: Closed): Boolean
83+
suspend fun coroutinesClassClosedBooleanResult(c: Class<Closed>): Boolean
8084
fun stringArray(a: Array<String>)
8185
fun argAndVararg(s: String, vararg a: String)
8286

tests/src/test/kotlin/test/MatchersTest.kt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package test
22

33
import com.nhaarman.expect.expect
44
import com.nhaarman.expect.expectErrorWithMessage
5+
import kotlinx.coroutines.test.runTest
56
import org.junit.Test
67
import org.mockito.ArgumentMatcher
78
import org.mockito.invocation.InvocationOnMock
@@ -35,6 +36,24 @@ class MatchersTest : TestBase() {
3536
}
3637
}
3738

39+
@Test
40+
fun anyClassClosedClass() {
41+
mock<Methods>().apply {
42+
classClosed(Closed::class.java)
43+
verify(this).classClosed(any())
44+
}
45+
}
46+
47+
@Test
48+
fun anyCoroutinesClosedClass() {
49+
mock<Methods>().apply {
50+
runTest {
51+
coroutinesClosed(Closed())
52+
verify(this@apply).coroutinesClosed(any())
53+
}
54+
}
55+
}
56+
3857
@Test
3958
fun anyIntArray() {
4059
mock<Methods>().apply {

tests/src/test/kotlin/test/MockingTest.kt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ package test
33
import com.nhaarman.expect.expect
44
import com.nhaarman.expect.expectErrorWithMessage
55
import com.nhaarman.expect.fail
6+
import kotlinx.coroutines.test.runTest
67
import org.mockito.kotlin.UseConstructor.Companion.parameterless
78
import org.mockito.kotlin.UseConstructor.Companion.withArguments
89
import org.mockito.kotlin.doReturn
910
import org.mockito.kotlin.mock
1011
import org.mockito.kotlin.verify
1112
import org.mockito.kotlin.whenever
13+
import org.mockito.kotlin.any
1214
import org.junit.Test
1315
import org.mockito.Mockito
1416
import org.mockito.exceptions.verification.WantedButNotInvoked
@@ -274,6 +276,34 @@ class MockingTest : TestBase() {
274276
expect(result).toBe("foo")
275277
}
276278

279+
@Test
280+
fun mockCoroutines_withClosedBooleanReturn_name() = runTest {
281+
/* Given */
282+
val mock = mock<Methods>(name = "myName") {
283+
onBlocking { coroutinesClosedBooleanResult(any()) } doReturn true
284+
}
285+
286+
/* When */
287+
val result = mock.coroutinesClosedBooleanResult(Closed())
288+
289+
/* Then */
290+
expect(result).toBe(true)
291+
}
292+
293+
@Test
294+
fun mockCoroutines_withClassClosedBooleanReturn_name() = runTest {
295+
/* Given */
296+
val mock = mock<Methods>(name = "myName") {
297+
onBlocking { coroutinesClassClosedBooleanResult(any()) } doReturn true
298+
}
299+
300+
/* When */
301+
val result = mock.coroutinesClassClosedBooleanResult(Closed::class.java)
302+
303+
/* Then */
304+
expect(result).toBe(true)
305+
}
306+
277307
@Test
278308
fun mockStubbing_withSettingsAPI_defaultAnswer() {
279309
/* Given */

0 commit comments

Comments
 (0)