【问题标题】:Android - Unit testing response handler class HttpException codesAndroid - 单元测试响应处理程序类 HttpException 代码
【发布时间】:2020-10-16 22:21:42
【问题描述】:

我正在编写一个类来对我的 ResponseHandler 类进行单元测试。我是否需要为我在 getErrorMessage 函数中考虑的每个可能的 HTTP 错误代码编写一个方法? 一个用于 400、401、403、404、503 等?

ResponseHandler.kt

enum class ErrorCodes(val code: Int) {
    SocketTimeOut(-1),
    NoConnection(0)
}

class ResponseHandler {

    fun <T : Any> handleSuccess(data: T): Resource<T> {
        return Resource.success(data)
    }

    fun <T : Any> handleException(e: Exception): Resource<T> {
        return when (e) {
            is HttpException -> Resource.error(getErrorMessage(e.code()), null)
            is SocketTimeoutException -> Resource.error(getErrorMessage(ErrorCodes.SocketTimeOut.code), null)
            is IOException -> Resource.error(getErrorMessage(ErrorCodes.NoConnection.code), null)
            else -> Resource.error(getErrorMessage(Int.MAX_VALUE), null)
        }
    }

    private fun getErrorMessage(code: Int): String {
        return when (code) {
            ErrorCodes.SocketTimeOut.code -> "Timeout"
            ErrorCodes.NoConnection.code -> "No Connection"
            HttpURLConnection.HTTP_BAD_REQUEST -> "Bad request"
            HttpURLConnection.HTTP_UNAUTHORIZED -> "Unauthorized"
            HttpURLConnection.HTTP_FORBIDDEN -> "Forbidden"
            HttpURLConnection.HTTP_NOT_FOUND -> "Not found"
            HttpURLConnection.HTTP_UNAVAILABLE -> "Service Unavailable"
            else -> "Something went wrong"
        }
    }
}

ResponseHandlerTest.kt

    @Test
    fun `when exception is HttpException and code is 404 then return Not found error message`() {
        val httpException = HttpException(Response.error<List<StylesData>>(HttpURLConnection.HTTP_NOT_FOUND, mock()))
        val result = responseHandler.handleException<List<StylesData>>(httpException)
        assertEquals("Not found", result.message)
    }

    @Test
    fun `when exception is SocketTimeoutException then return Timeout error message`() {
        val socketTimeoutException = SocketTimeoutException()
        val result = responseHandler.handleException<List<StylesData>>(socketTimeoutException)
        assertEquals("Timeout", result.message)
    }

    @Test
    fun `when exception is IOException then return No connection error message`() {
        val ioException = IOException()
        val result = responseHandler.handleException<List<StylesData>>(ioException)
        assertEquals("No Connection", result.message)
    }

【问题讨论】:

    标签: android unit-testing kotlin testing


    【解决方案1】:

    在这种情况下,是的,但我会考虑将该处理程序划分为特定情况,如下所示:

    @ControllerAdvice
    class RestResponseEntityExceptionHandler {
    
        @ExceptionHandler(IOException::class)
        fun handle(ex: IOException?): ResponseEntity<String> {
            // ...
        }
    }
    

    我还将错误消息移动到资源文件或枚举中,这更适合此操作。

    有了它,如果你想处理一个新的案例,你就不必接触你的代码,而且你也避免了一个不断增加的可能的异常列表。

    文档的相关部分是here

    【讨论】:

    • 抱歉,我猜我说的不够具体。该类是 Android 应用程序的一部分,因此我不能使用这些注释,因为它们是 Spring 注释。无论如何,如果它仍然适用,我认为我不完全理解您通过划分特定案例的意思。你的意思是把handleException方法分成3个方法,每个异常类型(IO、Http、SocketTO)一个?
    • 抱歉,我很困惑,我确定您在询问 [Spring],因为我正在过滤 [Spring] 问题。 :D
    猜你喜欢
    • 2014-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-08
    • 1970-01-01
    • 1970-01-01
    • 2017-01-22
    • 1970-01-01
    相关资源
    最近更新 更多