Skip to content

Commit

Permalink
Improve test coverage for coroutines (#526)
Browse files Browse the repository at this point in the history
  • Loading branch information
hadilq committed Sep 10, 2024
1 parent 391bb3f commit 176fb3a
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 0 deletions.
1 change: 1 addition & 0 deletions tests/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ dependencies {

testImplementation 'junit:junit:4.13.2'
testImplementation "com.nhaarman:expect.kt:1.0.1"
testImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:1.9.0-RC"
}

tasks.withType(KotlinCompile).configureEach {
Expand Down
4 changes: 4 additions & 0 deletions tests/src/test/kotlin/test/Classes.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ class Closed
interface Methods {

fun closed(c: Closed)
fun classClosed(c: Class<Closed>)
suspend fun coroutinesClosed(c: Closed)
fun closedArray(a: Array<Closed>)
fun closedNullableArray(a: Array<Closed?>)
fun closedCollection(c: Collection<Closed>)
Expand Down Expand Up @@ -77,6 +79,8 @@ interface Methods {
fun nullableStringResult(): String?
fun builderMethod(): Methods
fun varargBooleanResult(vararg values: String): Boolean
suspend fun coroutinesClosedBooleanResult(c: Closed): Boolean
suspend fun coroutinesClassClosedBooleanResult(c: Class<Closed>): Boolean
fun stringArray(a: Array<String>)
fun argAndVararg(s: String, vararg a: String)

Expand Down
19 changes: 19 additions & 0 deletions tests/src/test/kotlin/test/MatchersTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package test

import com.nhaarman.expect.expect
import com.nhaarman.expect.expectErrorWithMessage
import kotlinx.coroutines.test.runTest
import org.junit.Test
import org.mockito.ArgumentMatcher
import org.mockito.invocation.InvocationOnMock
Expand Down Expand Up @@ -35,6 +36,24 @@ class MatchersTest : TestBase() {
}
}

@Test
fun anyClassClosedClass() {
mock<Methods>().apply {
classClosed(Closed::class.java)
verify(this).classClosed(any())
}
}

@Test
fun anyCoroutinesClosedClass() {
mock<Methods>().apply {
runTest {
coroutinesClosed(Closed())
verify(this@apply).coroutinesClosed(any())
}
}
}

@Test
fun anyIntArray() {
mock<Methods>().apply {
Expand Down
30 changes: 30 additions & 0 deletions tests/src/test/kotlin/test/MockingTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ package test
import com.nhaarman.expect.expect
import com.nhaarman.expect.expectErrorWithMessage
import com.nhaarman.expect.fail
import kotlinx.coroutines.test.runTest
import org.mockito.kotlin.UseConstructor.Companion.parameterless
import org.mockito.kotlin.UseConstructor.Companion.withArguments
import org.mockito.kotlin.doReturn
import org.mockito.kotlin.mock
import org.mockito.kotlin.verify
import org.mockito.kotlin.whenever
import org.mockito.kotlin.any
import org.junit.Test
import org.mockito.Mockito
import org.mockito.exceptions.verification.WantedButNotInvoked
Expand Down Expand Up @@ -274,6 +276,34 @@ class MockingTest : TestBase() {
expect(result).toBe("foo")
}

@Test
fun mockCoroutines_withClosedBooleanReturn_name() = runTest {
/* Given */
val mock = mock<Methods>(name = "myName") {
onBlocking { coroutinesClosedBooleanResult(any()) } doReturn true
}

/* When */
val result = mock.coroutinesClosedBooleanResult(Closed())

/* Then */
expect(result).toBe(true)
}

@Test
fun mockCoroutines_withClassClosedBooleanReturn_name() = runTest {
/* Given */
val mock = mock<Methods>(name = "myName") {
onBlocking { coroutinesClassClosedBooleanResult(any()) } doReturn true
}

/* When */
val result = mock.coroutinesClassClosedBooleanResult(Closed::class.java)

/* Then */
expect(result).toBe(true)
}

@Test
fun mockStubbing_withSettingsAPI_defaultAnswer() {
/* Given */
Expand Down

0 comments on commit 176fb3a

Please sign in to comment.