Skip to content

Commit

Permalink
add more tests (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
MarvinSchramm authored Feb 15, 2021
1 parent a0b02ba commit 11b55b1
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 1 deletion.
1 change: 1 addition & 0 deletions spring-json-api/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
dependencies {
optional(group = "org.springframework.boot", name = "spring-boot-starter-web")
testImplementation("io.mockk:mockk:1.10.6")
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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))
}
}
Original file line number Diff line number Diff line change
@@ -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<HttpMessageConverter<String>>()::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)
}
}

0 comments on commit 11b55b1

Please sign in to comment.