-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a0b02ba
commit 11b55b1
Showing
4 changed files
with
94 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
spring-json-api/src/test/kotlin/io/hndrs/api/exception/ExceptionHandlerTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
spring-json-api/src/test/kotlin/io/hndrs/api/response/ResponseAdviceTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |