Hi, I would like to stub my custom http client , h...
# ktor
j
Hi, I would like to stub my custom http client , however i realized that it is not possible neither to create an interface with inline functions nor mock it with mockk. Is there any other way to encapsulate httpClient that doesn’t need inline/reifed ?
Copy code
class CustomHttpClient(
    val httpClient: HttpClient,
) {
    suspend inline fun <reified T> doRequest(
        uri: String,
        payload: Any,
        handleError: (error: Error, ex: ClientRequestException) -> T?
    ): T? {
       //execute httpclient request and deal with errors
 return try {
            <http://httpClient.post|httpClient.post>(uri) {
                body = payload
            }
        } catch (ex: ClientRequestException) {
          
        }
  
}
a
You can use the MockEngine to avoid making HTTP calls through a network and to fake responses.
j
yes, i had thought about that, however i was trying to follow “don’t mock what you don’t own” guide, writte an adapter and use it with my domain vocabulary, so that when i migrate to ktor 2 or other version i don’t have to fix my tests but only change adapter implementation. Using Mockengine involves that i need to write json code which is part of the api contract. Breakeages to the contract won’t be catch by those test, so i ’m not comfortable writing those tests.🤔. These will only test that my mappings are correct.