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

Spy Object Returns Unexpected Default Value Instead of Real Property #1201

Open
fightnyy opened this issue Jan 10, 2024 · 0 comments
Open

Spy Object Returns Unexpected Default Value Instead of Real Property #1201

fightnyy opened this issue Jan 10, 2024 · 0 comments

Comments

@fightnyy
Copy link

Expected Behavior

I expect the spy object to return the real property value of an entity. In this case, the id property of SimpleEntity should return 1L as explicitly set in the test setup.

Current Behavior

Instead of returning the real value, the spy object returns 0, which seems to be a default primitive type value. This behavior is inconsistent with my understanding of how spyk should function.

Context

MockK version: 1.13.8
Operating System: Mac M1 Max (Sonoma 14.2.1)
Kotlin version: 1.5.20
JDK version: 11
Type of test: Unit test
JUnit version is not relevant in this context.

Minimal Reproducible Code

Here is a minimal code snippet to reproduce the issue:

import javax.persistence.Entity
import javax.persistence.GeneratedValue
import javax.persistence.GenerationType
import javax.persistence.Id
@Entity
class SimpleEntity (
    @Id @GeneratedValue(strategy = GenerationType.IDENTITY) val id: Long,
)

import org.springframework.data.jpa.repository.JpaRepository

interface SimpleEntityRepository: JpaRepository<SimpleEntity, Long>

// Test Code
class ExampleTest : StringSpec({
    "exampleTest" {
        val simple = spyk(SimpleEntity(id = 1L))
        every {
            simpleEntityRepository.findById(simple.id).orElseThrow()
        } returns simple
        // Expecting id to be 1L, but the id in the 'every' block is 0
    }
})

I would appreciate any insights or suggestions on why this behavior occurs and how to resolve it.

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