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

mockkObject/every fails substituting object function call, seems to run method #1252

Open
3 tasks done
carstenhag opened this issue May 16, 2024 · 1 comment
Open
3 tasks done

Comments

@carstenhag
Copy link

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 -> Found some related issues to mockkObject but none describing my issue.

Repro Project

https://github.com/carstenhag/mockkobjectrepro/blob/master/app/src/test/java/com/example/mockkobjecttest/ExampleUnitTest.kt#L32

Expected Behavior

A mocked object's function which I am trying to change doesn't run before while I am calling every{}.

Current Behavior

The real function seems to be called while calling every{}.

Failure Information (for bugs)

Please help provide information about the failure if this is a bug. If it is not a bug, please remove the rest of this template.

Steps to Reproduce

Please provide detailed steps for reproducing the issue.

  1. Run mockkObject on object provided by Emarsys Android SDK
  2. Run every { emarsysObject.clearContact() } returns Unit
  3. Get a crash because it actually runs the real implementation (clearContact())

Context

  • MockK version: 1.13.10
  • OS: macOS 14.4.1
  • Kotlin version: 1.9.23
  • JDK version: 17
  • JUnit version: 4.13.2
  • Type of test: unit test

Stack trace

// -----------------------[ YOUR STACK STARTS HERE ] -----------------------
(exception caused by the real implementation, expected if you run it without initializing it)

java.lang.IllegalStateException: DependencyContainer has to be setup first!
	at com.emarsys.di.EmarsysComponentKt.emarsys(EmarsysComponent.kt:21)
	at com.emarsys.di.EmarsysDependencyInjection.mobileEngageApi(EmarsysDependencyInjection.kt:24)
	at com.emarsys.Emarsys.clearContact(Emarsys.kt:157)
	at com.emarsys.Emarsys.clearContact$default(Emarsys.kt:152)
	at com.example.mockkobjecttest.ExampleUnitTest$mockingEmarsys$1.invoke(ExampleUnitTest.kt:38)
	at com.example.mockkobjecttest.ExampleUnitTest$mockingEmarsys$1.invoke(ExampleUnitTest.kt:38)
// -----------------------[ YOUR STACK TRACE ENDS HERE ] -----------------------

Minimal reproducible code (the gist of this issue)

// -----------------------[ GRADLE DEFINITIONS ] -----------------------
dependencies {
    // Requires an android project / AGP
    implementation("io.mockk:mockk:1.13.10")
    implementation("com.emarsys:emarsys-sdk:3.7.5")
    coreLibraryDesugaring("com.android.tools:desugar_jdk_libs_nio:2.0.4")
}
// -----------------------[ YOUR CODE STARTS HERE ] -----------------------
package com.example.mockkobjecttest

import com.emarsys.Emarsys
import io.mockk.every
import io.mockk.mockkObject
import io.mockk.verify
import org.junit.Test

class Issue69Test {

    @Test
    fun mockingEmarsys() {
        val emarsysObject = Emarsys
        mockkObject(emarsysObject)
        every { emarsysObject.clearContact() } returns Unit

        emarsysObject.clearContact()

        verify(exactly = 1) { emarsysObject.clearContact() }
    }

}
// -----------------------[ YOUR CODE ENDS HERE ] -----------------------
@alexstarkqm
Copy link

Hi, I'm facing the same issue and wonder if this behavior is intended or a bug.

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