Android-单元测试响应处理程序类HttpException代码

Android-单元测试响应处理程序类HttpException代码,android,unit-testing,kotlin,testing,Android,Unit Testing,Kotlin,Testing,我正在写一个类来单元测试我的ResponseHandler类。我是否需要为getErrorMessage函数中考虑的每个可能的HTTP错误代码编写一个方法? 400、401、403、404、503等各一个 ResponseHandler.kt enum class ErrorCodes(val code: Int) { SocketTimeOut(-1), NoConnection(0) } class ResponseHandler { fun <T : An

我正在写一个类来单元测试我的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"
        }
    }
}
    @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)
    }
enum类错误代码(val代码:Int){
SocketTimeOut(-1),
无连接(0)
}
班级负责人{
趣味handleSuccess(数据:T):资源{
返回资源。成功(数据)
}
FunHandleException(e:异常):资源{
返回时(e){
是HttpException->Resource.error(getErrorMessage(e.code()),null)
是SocketTimeoutException->Resource.error(getErrorMessage(ErrorCodes.SocketTimeOut.code),null)
是IOException->Resource.error(getErrorMessage(ErrorCodes.NoConnection.code),null)
else->Resource.error(getErrorMessage(Int.MAX_值),null)
}
}
private fun getErrorMessage(代码:Int):字符串{
返回时间(代码){
ErrorCodes.SocketTimeOut.code->“超时”
ErrorCodes.NoConnection.code->“无连接”
HttpURLConnection.HTTP\u错误请求->“错误请求”
HttpURLConnection.HTTP_UNAUTHORIZED->“UNAUTHORIZED”
HttpURLConnection.HTTP_禁止->“禁止”
HttpURLConnection.HTTP_未找到->“未找到”
HttpURLConnection.HTTP_不可用->“服务不可用”
else->“出了点问题”
}
}
}
响应HandlerTest.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"
        }
    }
}
    @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)
    }
@测试
fun`当异常为HttpException且代码为404时,返回未找到的错误消息`(){
val httpException=httpException(Response.error(HttpURLConnection.HTTP_未找到,mock()))
val结果=responseHandler.handleException(httpException)
assertEquals(“未找到”,result.message)
}
@试验
fun`当异常为SocketTimeoutException时,返回超时错误消息`(){
val socketTimeoutException=socketTimeoutException()
val结果=responseHandler.handleException(socketTimeoutException)
assertEquals(“超时”,结果消息)
}
@试验
fun`当异常为IOException时,则不返回连接错误消息`(){
val ioException=ioException()
val结果=responseHandler.handleException(ioException)
assertEquals(“无连接”,result.message)
}

在这种情况下,是的,但是我会考虑把这个处理程序分成这样的具体情况:

@ControllerAdvice
类ResponseEntityExceptionHandler{
@ExceptionHandler(IOException::类)
有趣的句柄(例如:IOException?):ResponseEntity{
// ...
}
}
我还将错误消息移动到资源文件或枚举中,这更适合于此

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


文档的相关部分是

对不起,我想我不够具体。该类是Android应用程序的一部分,因此我不能使用这些注释,因为它们是Spring注释。无论如何,如果它仍然适用,我不认为我完全理解你将其划分为具体案例的意思。您的意思是将handleException方法分为3种方法,每种异常类型(IO、Http、SocketTO)一种方法?对不起,我很困惑,我确定您是在询问[Spring],因为我正在筛选[Spring]问题:D