8000 Field value is not set when property mockked · Issue #1371 · mockk/mockk · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Field value is not set when property mockked #1371

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

Open
Ufkoku opened this issue Apr 8, 2025 · 0 comments
Open

Field value is not set when property mockked #1371

Ufkoku opened this issue Apr 8, 2025 · 0 comments

Comments

@Ufkoku
Copy link
Ufkoku commented Apr 8, 2025

Code to reproduce the issue

abstract class Parent(dependencies: List<Parent>) {

    val dependencyFlows = dependencies.map(Parent::flow)

    val flow = MutableStateFlow(0)
//        get() = field //uncomment this line to fix the test
}

class Child1(child2: Child2) : Parent(listOf(child2)) {

}

class Child2() : Parent(emptyList()) {

}

class Tests {

    @Test
    fun test() {
        //Given
        val child2Flow = MutableStateFlow(5)
        val child2 = mockk<Child2> {
            every { flow } returns child2Flow
        }

        //When
        val child1 = Child1(child2)

        //Then
        assert(child1.dependencyFlows == listOf(child2Flow))
    }
}

Explanation

When you call child2.flow in the test it will return you child2Flow value.
But when Parent accesses flow it does it directly, without calling the property getter, and null is returned.
When even the empty getter is specified Parent starts accessing flow with specified getter, which is properly mockked with MockK

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
0