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

Cannot mock constructor in instrumented test #1251

Open
3 tasks done
SekoiaTree opened this issue May 15, 2024 · 0 comments
Open
3 tasks done

Cannot mock constructor in instrumented test #1251

SekoiaTree opened this issue May 15, 2024 · 0 comments

Comments

@SekoiaTree
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 (pretty sure but not 100%)

Expected Behavior

mockkConstructor should work on Android 13.

Current Behavior

Newly constructed objects are never spied on.

Failure Information (for bugs)

None visible; the objects simply do not run any every code, and simply function normally.

Steps to Reproduce

Define a class in your android package:

class MockCls {
  fun add(a: Int, b: Int) = a + b
}

In an instrumented test, mock the class' constructor, then create a new instance and print it:


mockkConstructor(MockCls::class)

every { anyConstructed<MockCls>().add(1, 2) } returns 4
println(MockCls())
assertEquals(4, MockCls().add(1, 2)) // note new object is created

The mocked object will not actually be mocked/spied, and the assertion will fail.

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.13.10
  • OS: Android 13.0 Tiramisu
  • Kotlin version: 1.9.0
  • JDK version: Unknown, not sure how to check that on android
  • JUnit version: 4.13.2
  • Type of test: android instrumented test

Failure Logs

N/A

Stack trace

N/A

Minimal reproducible code (the gist of this issue)

// In main app
package io.mockk.gh

class MockCls {
  fun add(a: Int, b: Int) = a + b
}

// In test package

package io.mockk.gh

import io.mockk.every
import io.mockk.mockk
import org.apache.kafka.common.KafkaFuture
import kotlin.test.Test

class Issue69Test {

    @Test
    fun test() {
        mockkConstructor(MockCls::class)

        every { anyConstructed<MockCls>().add(1, 2) } returns 4
        assertEquals(4, MockCls().add(1, 2)) // note new object is created. Test fails.
    }

}
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

1 participant