Polish "Introduce TestRestTemplate Kotlin extensions"

Closes gh-11039
pull/11135/merge
Stephane Nicoll 7 years ago
parent cd98da5ee9
commit aa87c45b5d

@ -75,6 +75,16 @@
<artifactId>hamcrest-library</artifactId> <artifactId>hamcrest-library</artifactId>
<optional>true</optional> <optional>true</optional>
</dependency> </dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-reflect</artifactId>
<optional>true</optional>
</dependency>
<dependency> <dependency>
<groupId>org.mockito</groupId> <groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId> <artifactId>mockito-core</artifactId>
@ -115,16 +125,6 @@
<artifactId>htmlunit</artifactId> <artifactId>htmlunit</artifactId>
<optional>true</optional> <optional>true</optional>
</dependency> </dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-reflect</artifactId>
<optional>true</optional>
</dependency>
<!-- Test --> <!-- Test -->
<dependency> <dependency>
<groupId>javax.json</groupId> <groupId>javax.json</groupId>

@ -22,7 +22,8 @@ import org.junit.Test
import org.junit.runner.RunWith import org.junit.runner.RunWith
import org.mockito.Answers import org.mockito.Answers
import org.mockito.Mock import org.mockito.Mock
import org.mockito.Mockito.* import org.mockito.Mockito.times
import org.mockito.Mockito.verify
import org.mockito.junit.MockitoJUnitRunner import org.mockito.junit.MockitoJUnitRunner
import org.springframework.core.ParameterizedTypeReference import org.springframework.core.ParameterizedTypeReference
import org.springframework.http.HttpEntity import org.springframework.http.HttpEntity
@ -183,7 +184,8 @@ class TestRestTemplateExtensionsTests {
val var1 = "var1" val var1 = "var1"
val var2 = "var2" val var2 = "var2"
template.exchange<List<Foo>>(url, method, entity, var1, var2) template.exchange<List<Foo>>(url, method, entity, var1, var2)
verify(template, times(1)).exchange(url, method, entity, object : ParameterizedTypeReference<List<Foo>>() {}, var1, var2) verify(template, times(1)).exchange(url, method, entity,
object : ParameterizedTypeReference<List<Foo>>() {}, var1, var2)
} }
@Test @Test
@ -193,7 +195,8 @@ class TestRestTemplateExtensionsTests {
val entity = mock<HttpEntity<Foo>>() val entity = mock<HttpEntity<Foo>>()
val vars = mapOf(Pair("key1", "value1"), Pair("key2", "value2")) val vars = mapOf(Pair("key1", "value1"), Pair("key2", "value2"))
template.exchange<List<Foo>>(url, method, entity, vars) template.exchange<List<Foo>>(url, method, entity, vars)
verify(template, times(1)).exchange(url, method, entity, object : ParameterizedTypeReference<List<Foo>>() {}, vars) verify(template, times(1)).exchange(url, method, entity,
object : ParameterizedTypeReference<List<Foo>>() {}, vars)
} }
@Test @Test
@ -202,26 +205,32 @@ class TestRestTemplateExtensionsTests {
val method = HttpMethod.GET val method = HttpMethod.GET
val entity = mock<HttpEntity<Foo>>() val entity = mock<HttpEntity<Foo>>()
template.exchange<List<Foo>>(url, method, entity) template.exchange<List<Foo>>(url, method, entity)
verify(template, times(1)).exchange(url, method, entity, object : ParameterizedTypeReference<List<Foo>>() {}) verify(template, times(1)).exchange(url, method, entity,
object : ParameterizedTypeReference<List<Foo>>() {})
} }
@Test @Test
fun `exchange with reified type parameters, String, HttpEntity`() { fun `exchange with reified type parameters, String, HttpEntity`() {
val entity = mock<RequestEntity<Foo>>() val entity = mock<RequestEntity<Foo>>()
template.exchange<List<Foo>>(entity) template.exchange<List<Foo>>(entity)
verify(template, times(1)).exchange(entity, object : ParameterizedTypeReference<List<Foo>>() {}) verify(template, times(1)).exchange(entity,
object : ParameterizedTypeReference<List<Foo>>() {})
} }
@Test @Test
fun `RestOperations are available`() { fun `RestOperations are available`() {
val extensions = Class.forName("org.springframework.boot.test.web.client.TestRestTemplateExtensionsKt") val extensions = Class.forName(
"org.springframework.boot.test.web.client.TestRestTemplateExtensionsKt")
ReflectionUtils.doWithMethods(RestOperations::class.java) { method -> ReflectionUtils.doWithMethods(RestOperations::class.java) { method ->
arrayOf(ParameterizedTypeReference::class, Class::class).forEach { kClass -> arrayOf(ParameterizedTypeReference::class, Class::class).forEach { kClass ->
if (method.parameterTypes.contains(kClass.java)) { if (method.parameterTypes.contains(kClass.java)) {
val parameters = mutableListOf<Class<*>>(TestRestTemplate::class.java).apply { addAll(method.parameterTypes.filter { it != kClass.java }) } val parameters = mutableListOf<Class<*>>(TestRestTemplate::class.java)
val f = extensions.getDeclaredMethod(method.name, *parameters.toTypedArray()).kotlinFunction!! .apply { addAll(method.parameterTypes.filter { it != kClass.java }) }
val f = extensions.getDeclaredMethod(method.name,
*parameters.toTypedArray()).kotlinFunction!!
Assert.assertEquals(1, f.typeParameters.size) Assert.assertEquals(1, f.typeParameters.size)
Assert.assertEquals(listOf(Any::class.createType()), f.typeParameters[0].upperBounds) Assert.assertEquals(listOf(Any::class.createType()),
f.typeParameters[0].upperBounds)
} }
} }
} }

Loading…
Cancel
Save