Hi folks! Given the following classes: ```class ...
# mockk
m
Hi folks! Given the following classes:
Copy code
class Connector() {
    val scanObservers = mutableListOf<(Scan) -> Unit>()
}
and
Copy code
class ScanProcessorViewModel constructor(connector: Connector) {
    init {
        connector.scanObservers.add {
            someMethod()
        }
    }
}
I have started a unit test including following properties:
Copy code
@RelaxedMockK
lateinit var connector: Connector

@SpyK
@InjectMockKs
lateinit var viewModel: ScanProcessorViewModel
How can I mock the
scanObservers
property and then verify that an entry has been added by the
init
block?
s
I’m not sure you need mocks here. What problems do you run into if you just use real instances?
m
The code - of course - is snipped… The viewmodel for example has a few more stuff provided by its constructor. Mocking these is much more comfortable - given the fact that the concrete implementations are in another package (following onion architecture). Of course - I could leave out the viewmodel property in my test and create it within my tests. This would make my
connector
able to be a concrete instance instead of a mock while the other stuff remains mocked.
OK - I need to revert my comment above. I need to mock the
connector
as well, as this class has an
init
block as well which does some concrete system calls (precisely: broadcasting intents)