title: Kotlin unit test Exception when mocking suspend function java.io.EOFException: Premature end of stream: expected 1 bytes
body: <p>I use <strong>KTor</strong> and <strong>Kotlin Serialization</strong> library in my android project, along with <strong>mockk</strong> and <strong>junit.jupiter</strong> for unit testing. I've encountered some problem when mocking ktor's <code>suspend</code> function <code>readText()</code>. The written unit test tests that <code>initErrorMessage()</code> function returns correct error message.</p>
  <p>Test class:</p>
  <pre><code>class ErrorTest {
  
      private val errorMessage = &quot;objectId must be provided.&quot;
      private val errorCode = 2689
      private val correctResponseJson = &quot;{\&quot;code\&quot;:$errorCode,\&quot;message\&quot;:\&quot;$errorMessage\&quot;}&quot;
      // ResponseException class is from ktor library
      private val exceptionMock: ResponseException = mockk(relaxed = true)
  
      @Test
      fun `initErrorMessage should return correct error message`() = runTest {
          coEvery { exceptionMock.response.readText() } returns correctResponseJson // &lt;-- here is the Error occurs
  
          val expectedError = errorMessage
          val actualError = initErrorMessage(exceptionMock)
  
          assertEquals(expectedError, actualError)
      }
  }
  </code></pre>
  <p>Method to test:</p>
  <pre><code>suspend fun initErrorMessage(cause: ResponseException): String {
      return try {
          val body = cause.response.readText()
          val jsonSerializer = JsonObject.serializer()
          val jsonObj = Json.decodeFromString(jsonSerializer, body)
          jsonObj[&quot;message&quot;].toString()
      } catch (e: Exception) {
          &quot;&quot;
      }
  }
  </code></pre>
  <p>During execution of the first line in the test method I get an Error:</p>
  <pre><code>Premature end of stream: expected 1 bytes
  java.io.EOFException: Premature end of stream: expected 1 bytes
      at io.ktor.utils.io.core.StringsKt.prematureEndOfStream(Strings.kt:492)
      at io.ktor.utils.io.core.internal.UnsafeKt.prepareReadHeadFallback(Unsafe.kt:78)
      at io.ktor.utils.io.core.internal.UnsafeKt.prepareReadFirstHead(Unsafe.kt:61)
      at io.ktor.utils.io.charsets.CharsetJVMKt.decode(CharsetJVM.kt:556)
      at io.ktor.utils.io.charsets.EncodingKt.decode(Encoding.kt:103)
      at io.ktor.utils.io.charsets.EncodingKt.decode$default(Encoding.kt:101)
      at io.ktor.client.statement.HttpStatementKt.readText(HttpStatement.kt:173)
      at io.ktor.client.statement.HttpStatementKt.readText$default(HttpStatement.kt:168)
      at com.example.android.http.error.ErrorTest$initErrorMessage should return correct error message$1$1.invokeSuspend(ErrorTest.kt:37)
      at com.example.android.http.error.ErrorTest$initErrorMessage should return correct error message$1$1.invoke(ErrorTest.kt)
      at com.example.android.http.error.ErrorTest$initErrorMessage should return correct error message$1$1.invoke(ErrorTest.kt)
      at io.mockk.impl.eval.RecordedBlockEvaluator$record$block$2$1.invokeSuspend(RecordedBlockEvaluator.kt:28)
      at io.mockk.impl.eval.RecordedBlockEvaluator$record$block$2$1.invoke(RecordedBlockEvaluator.kt)
      at io.mockk.InternalPlatformDsl$runCoroutine$1.invokeSuspend(InternalPlatformDsl.kt:20)
  </code></pre>
  <p>How to mock this <code>suspend</code> method <code>readText()</code> without an Error?</p>