Search code examples
kotlintestingexceptionbad-requesterror-code

Private function and null safe operator testing in Kotlin


I am trying to improve the test coverage, but I am confused about some lines that are considered as "not covered" or "only partially covered" such as:

400 -> throw BadRequestException(response.code(), response.errorBody()?.string())

and

if (response.code() !in 200..299) {
      throwError(response)
}

throwError is a private function. I am testing that, with a response code 204, there is a success event and 400, there is the right error that is thrown. But how to check that throwError is called?

Also, when I check that the error BadRequestException is thrown, why only 1 or 2 conditions are met?


Solution

  • In the first case, the two conditions should be:

    1. response.errorBody() is null
    2. response.errorBody() is not null

    So you need to cover both to have it completely covered.