From 11b55b10a23c77bd21e8fb399592d38062654af9 Mon Sep 17 00:00:00 2001 From: Marvin Schramm Date: Mon, 15 Feb 2021 20:01:53 +0100 Subject: [PATCH] add more tests (#4) --- spring-json-api/build.gradle.kts | 1 + .../hndrs/api/exception/ExceptionHandler.kt | 2 +- .../api/exception/ExceptionHandlerTest.kt | 54 +++++++++++++++++++ .../hndrs/api/response/ResponseAdviceTest.kt | 38 +++++++++++++ 4 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 spring-json-api/src/test/kotlin/io/hndrs/api/exception/ExceptionHandlerTest.kt create mode 100644 spring-json-api/src/test/kotlin/io/hndrs/api/response/ResponseAdviceTest.kt diff --git a/spring-json-api/build.gradle.kts b/spring-json-api/build.gradle.kts index dbeda30..35fe382 100644 --- a/spring-json-api/build.gradle.kts +++ b/spring-json-api/build.gradle.kts @@ -1,3 +1,4 @@ dependencies { optional(group = "org.springframework.boot", name = "spring-boot-starter-web") + testImplementation("io.mockk:mockk:1.10.6") } diff --git a/spring-json-api/src/main/kotlin/io/hndrs/api/exception/ExceptionHandler.kt b/spring-json-api/src/main/kotlin/io/hndrs/api/exception/ExceptionHandler.kt index 96ad6fd..e9a2202 100644 --- a/spring-json-api/src/main/kotlin/io/hndrs/api/exception/ExceptionHandler.kt +++ b/spring-json-api/src/main/kotlin/io/hndrs/api/exception/ExceptionHandler.kt @@ -26,7 +26,7 @@ class ExceptionHandler : ResponseEntityExceptionHandler() { .body(ErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR, ex.message)) } - override fun handleExceptionInternal( + public override fun handleExceptionInternal( ex: Exception, body: Any?, headers: HttpHeaders, diff --git a/spring-json-api/src/test/kotlin/io/hndrs/api/exception/ExceptionHandlerTest.kt b/spring-json-api/src/test/kotlin/io/hndrs/api/exception/ExceptionHandlerTest.kt new file mode 100644 index 0000000..d50c138 --- /dev/null +++ b/spring-json-api/src/test/kotlin/io/hndrs/api/exception/ExceptionHandlerTest.kt @@ -0,0 +1,54 @@ +package io.hndrs.api.exception + +import com.nhaarman.mockitokotlin2.mock +import io.hndrs.api.response.ErrorResponse +import org.junit.jupiter.api.Assertions +import org.junit.jupiter.api.Test +import org.springframework.http.HttpHeaders +import org.springframework.http.HttpStatus +import org.springframework.web.server.ResponseStatusException + +internal class ExceptionHandlerTest { + + + @Test + fun handleResponseStatusExceptions() { + val expectedStatus = HttpStatus.BAD_REQUEST + val expectedMessage = "Error Message" + + val resonse = ExceptionHandler() + .handleResponseStatusExceptions(ResponseStatusException(expectedStatus, expectedMessage), mock()) + + Assertions.assertEquals(expectedStatus, resonse.statusCode) + Assertions.assertEquals(resonse.body, ErrorResponse(expectedStatus, expectedMessage)) + } + + @Test + fun handleExceptions() { + val expectedMessage = "Error Message" + + val resonse = ExceptionHandler() + .handleExceptions(IllegalStateException(expectedMessage), mock()) + + Assertions.assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, resonse.statusCode) + Assertions.assertEquals(resonse.body, ErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR, expectedMessage)) + } + + @Test + fun handleExceptionsInternal() { + val expectedStatus = HttpStatus.BAD_REQUEST + val expectedMessage = "Error Message" + + val resonse = ExceptionHandler() + .handleExceptionInternal( + IllegalStateException(expectedMessage), + null, + HttpHeaders(), + expectedStatus, + mock() + ) + + Assertions.assertEquals(expectedStatus, resonse!!.statusCode) + Assertions.assertEquals(resonse!!.body, ErrorResponse(HttpStatus.BAD_REQUEST, expectedMessage)) + } +} diff --git a/spring-json-api/src/test/kotlin/io/hndrs/api/response/ResponseAdviceTest.kt b/spring-json-api/src/test/kotlin/io/hndrs/api/response/ResponseAdviceTest.kt new file mode 100644 index 0000000..2fd6e6a --- /dev/null +++ b/spring-json-api/src/test/kotlin/io/hndrs/api/response/ResponseAdviceTest.kt @@ -0,0 +1,38 @@ +package io.hndrs.api.response + +import io.mockk.mockk +import org.junit.jupiter.api.Assertions.assertFalse +import org.junit.jupiter.api.Assertions.assertTrue +import org.junit.jupiter.api.Test +import org.springframework.core.MethodParameter +import org.springframework.http.converter.HttpMessageConverter +import kotlin.reflect.jvm.javaMethod + +internal class ResponseAdviceTest { + + + @Test + fun supports() { + val mockConverterType = mockk>()::class.java + assertTrue(ResponseAdvice().supports(supportedMethod, mockConverterType)) + assertFalse(ResponseAdvice().supports(unsupportedMethod, mockConverterType)) + } + + + class TestClass { + + @JsonApiResponse + fun supportedMethod(): String { + return "" + } + + fun unsupportedMethod(): String { + return "" + } + } + + companion object { + val supportedMethod = MethodParameter(TestClass::supportedMethod.javaMethod!!, -1) + val unsupportedMethod = MethodParameter(TestClass::unsupportedMethod.javaMethod!!, -1) + } +}