Skip to content

Commit 11b55b1

Browse files
add more tests (#4)
1 parent a0b02ba commit 11b55b1

File tree

4 files changed

+94
-1
lines changed

4 files changed

+94
-1
lines changed

spring-json-api/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
dependencies {
22
optional(group = "org.springframework.boot", name = "spring-boot-starter-web")
3+
testImplementation("io.mockk:mockk:1.10.6")
34
}

spring-json-api/src/main/kotlin/io/hndrs/api/exception/ExceptionHandler.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class ExceptionHandler : ResponseEntityExceptionHandler() {
2626
.body(ErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR, ex.message))
2727
}
2828

29-
override fun handleExceptionInternal(
29+
public override fun handleExceptionInternal(
3030
ex: Exception,
3131
body: Any?,
3232
headers: HttpHeaders,
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package io.hndrs.api.exception
2+
3+
import com.nhaarman.mockitokotlin2.mock
4+
import io.hndrs.api.response.ErrorResponse
5+
import org.junit.jupiter.api.Assertions
6+
import org.junit.jupiter.api.Test
7+
import org.springframework.http.HttpHeaders
8+
import org.springframework.http.HttpStatus
9+
import org.springframework.web.server.ResponseStatusException
10+
11+
internal class ExceptionHandlerTest {
12+
13+
14+
@Test
15+
fun handleResponseStatusExceptions() {
16+
val expectedStatus = HttpStatus.BAD_REQUEST
17+
val expectedMessage = "Error Message"
18+
19+
val resonse = ExceptionHandler()
20+
.handleResponseStatusExceptions(ResponseStatusException(expectedStatus, expectedMessage), mock())
21+
22+
Assertions.assertEquals(expectedStatus, resonse.statusCode)
23+
Assertions.assertEquals(resonse.body, ErrorResponse(expectedStatus, expectedMessage))
24+
}
25+
26+
@Test
27+
fun handleExceptions() {
28+
val expectedMessage = "Error Message"
29+
30+
val resonse = ExceptionHandler()
31+
.handleExceptions(IllegalStateException(expectedMessage), mock())
32+
33+
Assertions.assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, resonse.statusCode)
34+
Assertions.assertEquals(resonse.body, ErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR, expectedMessage))
35+
}
36+
37+
@Test
38+
fun handleExceptionsInternal() {
39+
val expectedStatus = HttpStatus.BAD_REQUEST
40+
val expectedMessage = "Error Message"
41+
42+
val resonse = ExceptionHandler()
43+
.handleExceptionInternal(
44+
IllegalStateException(expectedMessage),
45+
null,
46+
HttpHeaders(),
47+
expectedStatus,
48+
mock()
49+
)
50+
51+
Assertions.assertEquals(expectedStatus, resonse!!.statusCode)
52+
Assertions.assertEquals(resonse!!.body, ErrorResponse(HttpStatus.BAD_REQUEST, expectedMessage))
53+
}
54+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package io.hndrs.api.response
2+
3+
import io.mockk.mockk
4+
import org.junit.jupiter.api.Assertions.assertFalse
5+
import org.junit.jupiter.api.Assertions.assertTrue
6+
import org.junit.jupiter.api.Test
7+
import org.springframework.core.MethodParameter
8+
import org.springframework.http.converter.HttpMessageConverter
9+
import kotlin.reflect.jvm.javaMethod
10+
11+
internal class ResponseAdviceTest {
12+
13+
14+
@Test
15+
fun supports() {
16+
val mockConverterType = mockk<HttpMessageConverter<String>>()::class.java
17+
assertTrue(ResponseAdvice().supports(supportedMethod, mockConverterType))
18+
assertFalse(ResponseAdvice().supports(unsupportedMethod, mockConverterType))
19+
}
20+
21+
22+
class TestClass {
23+
24+
@JsonApiResponse
25+
fun supportedMethod(): String {
26+
return ""
27+
}
28+
29+
fun unsupportedMethod(): String {
30+
return ""
31+
}
32+
}
33+
34+
companion object {
35+
val supportedMethod = MethodParameter(TestClass::supportedMethod.javaMethod!!, -1)
36+
val unsupportedMethod = MethodParameter(TestClass::unsupportedMethod.javaMethod!!, -1)
37+
}
38+
}

0 commit comments

Comments
 (0)