channel iterator partially covered [1 of 2 branches] kotlin

205 Views Asked by At

I have written a custom SQS consumer wherein :

  • One coroutine that periodically retrieves the messages
  • Multiple workers that process the receiving messages in parallel without blocking.
  • A channel to communicate between the MsgReceiver coroutine and the Workers.

However, when writing unit tests I am unable to achieve 100% test coverage kover or with sonar. The kover/sonar plugin complains about partially covered while loop [highlighted in yellow] and I cannot understand which scenario have I missed

Unit test cases:

  1. When Single message
  2. When no message
  3. When 10+ message
  4. when "no more" message after polling once
  5. When an error

Actual code :

enter image description here

I looked inside of itr.hasnext() method and there are several branches but unable to understand why code coverage is considering internal methods of the library for code coverage ?

Thanks in advance

1

There are 1 best solutions below

0
Pranav On

Found the issue:

Basically, the hasNext() method from AbstractChannel class never returned false.

enter image description here

So, mocked the channel, wrote a custom iterator and made hasNext() return false

val mockChannel: Channel<Message> = mock()
whenever(mockChannel.iterator()).thenReturn(Itr())

enter image description here

And it worked like a charm

enter image description here