17
17
package org.springframework.web.client
18
18
19
19
import com.nhaarman.mockito_kotlin.mock
20
+ import org.junit.Assert
20
21
import org.junit.Test
21
22
import org.junit.runner.RunWith
22
23
import org.mockito.Answers
@@ -27,7 +28,10 @@ import org.springframework.core.ParameterizedTypeReference
27
28
import org.springframework.http.HttpEntity
28
29
import org.springframework.http.HttpMethod
29
30
import org.springframework.http.RequestEntity
31
+ import org.springframework.util.ReflectionUtils
30
32
import java.net.URI
33
+ import kotlin.reflect.full.createType
34
+ import kotlin.reflect.jvm.kotlinFunction
31
35
32
36
/* *
33
37
* Mock object based tests for [RestOperations] Kotlin extensions.
@@ -64,6 +68,13 @@ class RestOperationsExtensionsTests {
64
68
verify(template, times(1 )).getForObject(url, Foo ::class .java)
65
69
}
66
70
71
+ @Test
72
+ fun `getForEntity with reified type parameters, String and URI` () {
73
+ val url = URI (" https://spring.io" )
74
+ template.getForEntity<Foo >(url)
75
+ verify(template, times(1 )).getForEntity(url, Foo ::class .java)
76
+ }
77
+
67
78
@Test
68
79
fun `getForEntity with reified type parameters, String and varargs` () {
69
80
val url = " https://spring.io"
@@ -73,6 +84,41 @@ class RestOperationsExtensionsTests {
73
84
verify(template, times(1 )).getForEntity(url, Foo ::class .java, var1, var2)
74
85
}
75
86
87
+ @Test
88
+ fun `getForEntity with reified type parameters and Map` () {
89
+ val url = " https://spring.io"
90
+ val vars = mapOf (Pair (" key1" , " value1" ), Pair (" key2" , " value2" ))
91
+ template.getForEntity<Foo >(url, vars)
92
+ verify(template, times(1 )).getForEntity(url, Foo ::class .java, vars)
93
+ }
94
+
95
+ @Test
96
+ fun `patchForObject with reified type parameters, String and varargs` () {
97
+ val url = " https://spring.io"
98
+ val body: Any = " body"
99
+ val var1 = " var1"
100
+ val var2 = " var2"
101
+ template.patchForObject<Foo >(url, body, var1, var2)
102
+ verify(template, times(1 )).patchForObject(url, body, Foo ::class .java, var1, var2)
103
+ }
104
+
105
+ @Test
106
+ fun `patchForObject with reified type parameters, String and Map` () {
107
+ val url = " https://spring.io"
108
+ val body: Any = " body"
109
+ val vars = mapOf (Pair (" key1" , " value1" ), Pair (" key2" , " value2" ))
110
+ template.patchForObject<Foo >(url, body, vars)
111
+ verify(template, times(1 )).patchForObject(url, body, Foo ::class .java, vars)
112
+ }
113
+
114
+ @Test
115
+ fun `patchForObject with reified type parameters` () {
116
+ val url = " https://spring.io"
117
+ val body: Any = " body"
118
+ template.patchForObject<Foo >(url, body)
119
+ verify(template, times(1 )).patchForObject(url, body, Foo ::class .java)
120
+ }
121
+
76
122
@Test
77
123
fun `postForObject with reified type parameters, String and varargs` () {
78
124
val url = " https://spring.io"
@@ -168,6 +214,21 @@ class RestOperationsExtensionsTests {
168
214
object : ParameterizedTypeReference <List <Foo >>() {})
169
215
}
170
216
217
+ @Test
218
+ fun `RestOperations are available` () {
219
+ val extensions = Class .forName(" org.springframework.web.client.RestOperationsExtensionsKt" )
220
+ ReflectionUtils .doWithMethods(RestOperations ::class .java) { method ->
221
+ arrayOf(ParameterizedTypeReference ::class , Class ::class ).forEach { kClass ->
222
+ if (method.parameterTypes.contains(kClass.java)) {
223
+ val parameters = mutableListOf<Class <* >>(RestOperations ::class .java).apply { addAll(method.parameterTypes.filter { it != kClass.java }) }
224
+ val f = extensions.getDeclaredMethod(method.name, * parameters.toTypedArray()).kotlinFunction!!
225
+ Assert .assertEquals(1 , f.typeParameters.size)
226
+ Assert .assertEquals(listOf (Any ::class .createType()), f.typeParameters[0 ].upperBounds)
227
+ }
228
+ }
229
+ }
230
+ }
231
+
171
232
class Foo
172
233
173
234
}
0 commit comments