Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kotlin coroutines and generic tests #526

Merged
merged 2 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading