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

spyk behaves different if an objectToCopy is passed #1262

Open
nilswieber opened this issue Jun 6, 2024 · 0 comments
Open

spyk behaves different if an objectToCopy is passed #1262

nilswieber opened this issue Jun 6, 2024 · 0 comments

Comments

@nilswieber
Copy link

Expected Behavior

A mock produced by spyk with an objectToCopy should be the same as if it this object is instantiated inside the spyk method itself (using a default constructor)

Current Behavior

Mocked properties are used if spyk instantiates the object itself but aren't if an instance is passed as objectToCopy and a method is called, which uses these properties internally.

Steps to Reproduce

Please provide detailed steps for reproducing the issue.

  1. provide an objectToCopy to spyk<...>(...)
  2. call a method, which isn't mocked but uses the mocked properties
  3. original properties of the object are used instead of the mocked ones

Context

  • MockK version: 1.13.11
  • OS: macOS Sonoma 14.3.1
  • Kotlin version: 1.9.22
  • JDK version: 17 (Temurin)
  • JUnit version: /
  • Type of test: /

Failure Logs

Console output for code below

John
John Doe
John
Alan Turing

Minimal reproducible code (the gist of this issue)

// -----------------------[ GRADLE DEFINITIONS ] -----------------------
dependencies {
    testImplementation "io.mockk:mockk:1.13.11"
}
// -----------------------[ YOUR CODE STARTS HERE ] -----------------------
class Person{
    val firstName:String = "Alan"
    val surname:String = "Turing"

    val fullname by lazy {
        "$firstName $surname"
    }
}


fun main() {
    val mockedPersonWorking = spyk<Person>(){
        every { firstName } returns "John"
        every { surname } returns "Doe"
    }

    println(mockedPersonWorking.firstName) // John
    println(mockedPersonWorking.fullname)  // John Doe

    val mockedPersonFailing = spyk<Person>(Person()){
        every { firstName } returns "John"
        every { surname } returns "Doe"
    }

    println(mockedPersonFailing.firstName) // John
    println(mockedPersonFailing.fullname)  // Alan Turing <---- Should be John Doe
}
// -----------------------[ YOUR CODE ENDS HERE ] -----------------------
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