Hi everyone !
Is it possible to test following piece of code via mocks ?
Copy code
process
.inputStream
.bufferedReader()
.useLines { seq ->
val summary = seq.find { line -> line.contains(KEY_PACKET_LOSS) }
if (summary != null) {
val loss = packetLoss(summary)
val result = when {
loss > acceptablePacketLoss -> Stability.UNSTABLE
loss == MAX_LOSS -> Stability.NO_CONNECTION
else -> Stability.STABLE
}
continuation.resumeIfActive(result)
} else {
continuation.resumeWithExceptionIfActive(
IllegalStateException("Couldn't find result line in ping command")
)
}
}
I added required statics, but
bufferedReader
and
useLines
are inline methods… can I work this around somehow ?
m
Mattia Tommasone
03/04/2021, 3:55 PM
mmm
not sure about it
how about extracting your block inside
useLines
to a separate function and testing it separately?
idk if it’s feasible, but it looks like the easiest solution to me, especially since
useLines
is inline and, thus, not mockable
m
Melih Aksoy
03/04/2021, 4:00 PM
yeah, seems like the only option, wanted to check if I can actually pass my mocked streams as there are few more
useLines
but better than skipping due to inline 😄 thanks ! 🙇
c
christophsturm
03/05/2021, 8:30 AM
you don’t need mocks to test that. mocks are for testing interactions. no need to mock data structures. just use a bytearray input stream