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

Mockk return Result<Result<T>> for Result<T> #375

Closed
3 tasks done
superbmilkyway opened this issue Oct 29, 2019 · 1 comment
Closed
3 tasks done

Mockk return Result<Result<T>> for Result<T> #375

superbmilkyway opened this issue Oct 29, 2019 · 1 comment

Comments

@superbmilkyway
Copy link

superbmilkyway commented Oct 29, 2019

Prerequisites

Please answer the following questions for yourself before submitting an issue.

  • I am running the latest version
  • I checked the documentation and found no answer
  • I checked to make sure that this issue has not already been filed

Expected Behavior

Mockk should return Result

Current Behavior

Based on the tutorial, I though I can use return Result type using Mockk

however it is not possible, mockk returns Result<Result<T>> rather than Result<T>

Steps to Reproduce

excute the following unit test

Context

Please provide any relevant information about your setup. This is important in case the issue is not reproducible except for under certain conditions.

  • MockK version: 1.9.3
  • OS: mac 10.14.6
  • Kotlin version: 1.3.41
  • JDK version: 11.0.4.hs-adpt
  • JUnit version: 5.3.4.2
  • Type of test: unit test

Failure Logs

java.lang.reflect.InvocationTargetException
	at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
	at kotlin.reflect.jvm.internal.calls.CallerImpl$Constructor.call(CallerImpl.kt:41)
	at kotlin.reflect.jvm.internal.KCallableImpl.call(KCallableImpl.kt:106)
	at kotlin.reflect.jvm.internal.KCallableImpl.callDefaultMethod$kotlin_reflection(KCallableImpl.kt:152)
	at kotlin.reflect.jvm.internal.KCallableImpl.callBy(KCallableImpl.kt:110)
	at kotlin.reflect.full.KClasses.createInstance(KClasses.kt:283)
	at io.kotlintest.runner.jvm.JvmKt.instantiateSpec(jvm.kt:15)
	at io.kotlintest.runner.jvm.TestEngine.createSpec(TestEngine.kt:122)
	at io.kotlintest.runner.jvm.TestEngine.access$createSpec(TestEngine.kt:19)
	at io.kotlintest.runner.jvm.TestEngine$submitSpec$1.run(TestEngine.kt:105)
	at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
	at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
	at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.lang.ClassCastException: class kotlin.Result cannot be cast to class java.lang.String (kotlin.Result is in unnamed module of loader 'app'; java.lang.String is in module java.base of loader 'bootstrap')
	at MockkTest.<init>(MockkTest.kt:16)
	... 18 more

Minimal reproducible code (the gist of this issue)

// -----------------------[ GRADLE DEFINITIONS ] -----------------------
val kotlinVersion = "1.3.41"
dependencies {
    implementation(kotlin("stdlib-jdk8"))
    testCompile("org.jetbrains.kotlin:kotlin-test:$kotlinVersion")
    testCompile("org.jetbrains.kotlin:kotlin-test-junit:$kotlinVersion")
    testImplementation("io.kotlintest:kotlintest-runner-junit5:3.4.2")
    testImplementation("org.assertj:assertj-core:3.14.0")

    testImplementation("io.mockk:mockk:1.9.3")
}

tasks.withType<Test> {
    useJUnitPlatform()
}

tasks.withType<KotlinCompile> {
    kotlinOptions {
        freeCompilerArgs = listOf("-Xjsr305=strict", "-Xallow-result-return-type")
        jvmTarget = "11"
    }
}
// -----------------------[ YOUR CODE STARTS HERE ] -----------------------
import io.kotlintest.specs.StringSpec
import io.mockk.every
import io.mockk.mockk
import org.assertj.core.api.Assertions.assertThat


interface ResultTester {
    fun get(name: String): Result<String>
}

class MockkTest : StringSpec() {
    init {
        val tester = mockk<ResultTester>()
        every { tester.get(any()) } returns Result.success("success")

        assertThat(tester.get("name").getOrNull()).isEqualTo("success")
    }
}
// -----------------------[ YOUR CODE ENDS HERE ] -----------------------
@oleksiyp
Copy link
Collaborator

oleksiyp commented Nov 2, 2019

Corresponds to #152, closing as duplicate

@oleksiyp oleksiyp closed this as completed Nov 2, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants