How do I test the `init block`? the sample: ```cl...
# test
s
How do I test the
init block
? the sample:
Copy code
class Foo() {
    init {
        loadData()
    }

    private fun loadData() {
        repository.getData()
    }
}
And this is what I done. Is it correct?
Copy code
class FooTest {

    private val foo by lazy { Foo() }

    @Test
    fun `test foo init block`() {

        foo // access foo to create an instance by Lazy 

        coVerify(exactly = 1) { repository.getData() }
    }
}
t
Why would you? It appears to have no effect
d
Best don't use lazy in tests. Since you want a fresh instance of it in each test. So either create it in @Before or use @InjectMocks as the alternative
e
I would prefer to mock the response of
repository.getData()
and verify that the data has been loaded as expected, if possible 🙂
171 Views