https://kotlinlang.org logo
#io
Title
# io
w

Wouter De Vos

10/16/2023, 2:28 PM
Are there any resources or suggestions out there on how to unit test stdin methods like 'readln'? I see some suggestions online on how to do this with Java API's like 'Scanner' and 'BufferedReader' but there is a real dearth of information on how to do this with the Kotlin 'Console' API. I currently have some logic in an infinite loop which awaits input from stdin before proceeding. I have wrapped 'readln' in a class function which is derived from an interface. I am then using this interface to mock stdin for unit tests but I am at a bit of a loss as to how I await input.
f

Filipp Zhinkin

10/17/2023, 8:07 AM
I am then using this interface to mock stdin for unit tests but I am at a bit of a loss as to how I await input.
Without a context, I would suggest creating a synchronization protocol using Channel. For example, you can use one channel to notify a "control" coroutine that the execution has reached the
readln
and then start waiting for data from another data-channel. And the control-coroutine may eventually (depending on how long you need a client code to await for data from
readln
) send data to the data-channel unblocking the
readln
.
w

Wouter De Vos

10/18/2023, 9:52 AM
Thanks, I’ll look into that. In the meantime I’ve done some refactoring which has improved the testability quite a lot
👍 1