From aa87c45b5d0014687295f1d4b1a99fe2d3b2ea71 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Fri, 24 Nov 2017 09:56:47 +0100 Subject: [PATCH] Polish "Introduce TestRestTemplate Kotlin extensions" Closes gh-11039 --- spring-boot-project/spring-boot-test/pom.xml | 20 +++++----- .../web/client/TestRestTemplateExtensions.kt | 38 +++++++++---------- .../client/TestRestTemplateExtensionsTests.kt | 27 ++++++++----- 3 files changed, 47 insertions(+), 38 deletions(-) diff --git a/spring-boot-project/spring-boot-test/pom.xml b/spring-boot-project/spring-boot-test/pom.xml index 32f2f16178..1ea05b8652 100644 --- a/spring-boot-project/spring-boot-test/pom.xml +++ b/spring-boot-project/spring-boot-test/pom.xml @@ -75,6 +75,16 @@ hamcrest-library true + + org.jetbrains.kotlin + kotlin-stdlib + true + + + org.jetbrains.kotlin + kotlin-reflect + true + org.mockito mockito-core @@ -115,16 +125,6 @@ htmlunit true - - org.jetbrains.kotlin - kotlin-stdlib - true - - - org.jetbrains.kotlin - kotlin-reflect - true - javax.json diff --git a/spring-boot-project/spring-boot-test/src/main/kotlin/org/springframework/boot/test/web/client/TestRestTemplateExtensions.kt b/spring-boot-project/spring-boot-test/src/main/kotlin/org/springframework/boot/test/web/client/TestRestTemplateExtensions.kt index 4884846233..747fae8572 100644 --- a/spring-boot-project/spring-boot-test/src/main/kotlin/org/springframework/boot/test/web/client/TestRestTemplateExtensions.kt +++ b/spring-boot-project/spring-boot-test/src/main/kotlin/org/springframework/boot/test/web/client/TestRestTemplateExtensions.kt @@ -32,7 +32,7 @@ import java.net.URI * @since 2.0.0 */ @Throws(RestClientException::class) -inline fun TestRestTemplate.getForObject(url: String, vararg uriVariables: Any): T? = +inline fun TestRestTemplate.getForObject(url: String, vararg uriVariables: Any): T? = getForObject(url, T::class.java, *uriVariables) /** @@ -43,7 +43,7 @@ inline fun TestRestTemplate.getForObject(url: String, vararg ur * @since 2.0.0 */ @Throws(RestClientException::class) -inline fun TestRestTemplate.getForObject(url: String, uriVariables: Map): T? = +inline fun TestRestTemplate.getForObject(url: String, uriVariables: Map): T? = getForObject(url, T::class.java, uriVariables) /** @@ -54,7 +54,7 @@ inline fun TestRestTemplate.getForObject(url: String, uriVariab * @since 2.0.0 */ @Throws(RestClientException::class) -inline fun TestRestTemplate.getForObject(url: URI): T? = +inline fun TestRestTemplate.getForObject(url: URI): T? = getForObject(url, T::class.java) /** @@ -65,7 +65,7 @@ inline fun TestRestTemplate.getForObject(url: URI): T? = * @since 2.0.0 */ @Throws(RestClientException::class) -inline fun TestRestTemplate.getForEntity(url: URI): ResponseEntity = +inline fun TestRestTemplate.getForEntity(url: URI): ResponseEntity = getForEntity(url, T::class.java) /** @@ -76,7 +76,7 @@ inline fun TestRestTemplate.getForEntity(url: URI): ResponseEnt * @since 2.0.0 */ @Throws(RestClientException::class) -inline fun TestRestTemplate.getForEntity(url: String, vararg uriVariables: Any): ResponseEntity = +inline fun TestRestTemplate.getForEntity(url: String, vararg uriVariables: Any): ResponseEntity = getForEntity(url, T::class.java, *uriVariables) /** @@ -87,7 +87,7 @@ inline fun TestRestTemplate.getForEntity(url: String, vararg ur * @since 2.0.0 */ @Throws(RestClientException::class) -inline fun TestRestTemplate.getForEntity(url: String, uriVariables: Map): ResponseEntity = +inline fun TestRestTemplate.getForEntity(url: String, uriVariables: Map): ResponseEntity = getForEntity(url, T::class.java, uriVariables) /** @@ -98,7 +98,7 @@ inline fun TestRestTemplate.getForEntity(url: String, uriVariab * @since 2.0.0 */ @Throws(RestClientException::class) -inline fun TestRestTemplate.patchForObject(url: String, request: Any, vararg uriVariables: Any): T? = +inline fun TestRestTemplate.patchForObject(url: String, request: Any, vararg uriVariables: Any): T? = patchForObject(url, request, T::class.java, *uriVariables) /** @@ -109,7 +109,7 @@ inline fun TestRestTemplate.patchForObject(url: String, request * @since 2.0.0 */ @Throws(RestClientException::class) -inline fun TestRestTemplate.patchForObject(url: String, request: Any, uriVariables: Map): T? = +inline fun TestRestTemplate.patchForObject(url: String, request: Any, uriVariables: Map): T? = patchForObject(url, request, T::class.java, uriVariables) /** @@ -120,7 +120,7 @@ inline fun TestRestTemplate.patchForObject(url: String, request * @since 2.0.0 */ @Throws(RestClientException::class) -inline fun TestRestTemplate.patchForObject(url: URI, request: Any): T? = +inline fun TestRestTemplate.patchForObject(url: URI, request: Any): T? = patchForObject(url, request, T::class.java) /** @@ -131,7 +131,7 @@ inline fun TestRestTemplate.patchForObject(url: URI, request: A * @since 2.0.0 */ @Throws(RestClientException::class) -inline fun TestRestTemplate.postForObject(url: String, request: Any, vararg uriVariables: Any): T? = +inline fun TestRestTemplate.postForObject(url: String, request: Any, vararg uriVariables: Any): T? = postForObject(url, request, T::class.java, *uriVariables) /** @@ -142,7 +142,7 @@ inline fun TestRestTemplate.postForObject(url: String, request: * @since 2.0.0 */ @Throws(RestClientException::class) -inline fun TestRestTemplate.postForObject(url: String, request: Any, uriVariables: Map): T? = +inline fun TestRestTemplate.postForObject(url: String, request: Any, uriVariables: Map): T? = postForObject(url, request, T::class.java, uriVariables) /** @@ -153,7 +153,7 @@ inline fun TestRestTemplate.postForObject(url: String, request: * @since 2.0.0 */ @Throws(RestClientException::class) -inline fun TestRestTemplate.postForObject(url: URI, request: Any): T? = +inline fun TestRestTemplate.postForObject(url: URI, request: Any): T? = postForObject(url, request, T::class.java) /** @@ -164,7 +164,7 @@ inline fun TestRestTemplate.postForObject(url: URI, request: An * @since 2.0.0 */ @Throws(RestClientException::class) -inline fun TestRestTemplate.postForEntity(url: String, request: Any, vararg uriVariables: Any): ResponseEntity = +inline fun TestRestTemplate.postForEntity(url: String, request: Any, vararg uriVariables: Any): ResponseEntity = postForEntity(url, request, T::class.java, *uriVariables) /** @@ -175,7 +175,7 @@ inline fun TestRestTemplate.postForEntity(url: String, request: * @since 2.0.0 */ @Throws(RestClientException::class) -inline fun TestRestTemplate.postForEntity(url: String, request: Any, uriVariables: Map): ResponseEntity = +inline fun TestRestTemplate.postForEntity(url: String, request: Any, uriVariables: Map): ResponseEntity = postForEntity(url, request, T::class.java, uriVariables) /** @@ -186,7 +186,7 @@ inline fun TestRestTemplate.postForEntity(url: String, request: * @since 2.0.0 */ @Throws(RestClientException::class) -inline fun TestRestTemplate.postForEntity(url: URI, request: Any): ResponseEntity = +inline fun TestRestTemplate.postForEntity(url: URI, request: Any): ResponseEntity = postForEntity(url, request, T::class.java) /** @@ -197,7 +197,7 @@ inline fun TestRestTemplate.postForEntity(url: URI, request: An * @since 2.0.0 */ @Throws(RestClientException::class) -inline fun TestRestTemplate.exchange(url: String, method: HttpMethod, requestEntity: HttpEntity<*>, vararg uriVariables: Any): ResponseEntity = +inline fun TestRestTemplate.exchange(url: String, method: HttpMethod, requestEntity: HttpEntity<*>, vararg uriVariables: Any): ResponseEntity = exchange(url, method, requestEntity, object : ParameterizedTypeReference() {}, *uriVariables) /** @@ -208,7 +208,7 @@ inline fun TestRestTemplate.exchange(url: String, method: HttpM * @since 2.0.0 */ @Throws(RestClientException::class) -inline fun TestRestTemplate.exchange(url: String, method: HttpMethod, requestEntity: HttpEntity<*>, uriVariables: Map): ResponseEntity = +inline fun TestRestTemplate.exchange(url: String, method: HttpMethod, requestEntity: HttpEntity<*>, uriVariables: Map): ResponseEntity = exchange(url, method, requestEntity, object : ParameterizedTypeReference() {}, uriVariables) /** @@ -219,7 +219,7 @@ inline fun TestRestTemplate.exchange(url: String, method: HttpM * @since 2.0.0 */ @Throws(RestClientException::class) -inline fun TestRestTemplate.exchange(url: URI, method: HttpMethod, requestEntity: HttpEntity<*>): ResponseEntity = +inline fun TestRestTemplate.exchange(url: URI, method: HttpMethod, requestEntity: HttpEntity<*>): ResponseEntity = exchange(url, method, requestEntity, object : ParameterizedTypeReference() {}) /** @@ -230,5 +230,5 @@ inline fun TestRestTemplate.exchange(url: URI, method: HttpMeth * @since 2.0.0 */ @Throws(RestClientException::class) -inline fun TestRestTemplate.exchange(requestEntity: RequestEntity<*>): ResponseEntity = +inline fun TestRestTemplate.exchange(requestEntity: RequestEntity<*>): ResponseEntity = exchange(requestEntity, object : ParameterizedTypeReference() {}) \ No newline at end of file diff --git a/spring-boot-project/spring-boot-test/src/test/kotlin/org/springframework/boot/test/web/client/TestRestTemplateExtensionsTests.kt b/spring-boot-project/spring-boot-test/src/test/kotlin/org/springframework/boot/test/web/client/TestRestTemplateExtensionsTests.kt index f8b5b0195e..d9ae59c7fb 100644 --- a/spring-boot-project/spring-boot-test/src/test/kotlin/org/springframework/boot/test/web/client/TestRestTemplateExtensionsTests.kt +++ b/spring-boot-project/spring-boot-test/src/test/kotlin/org/springframework/boot/test/web/client/TestRestTemplateExtensionsTests.kt @@ -22,7 +22,8 @@ import org.junit.Test import org.junit.runner.RunWith import org.mockito.Answers 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.springframework.core.ParameterizedTypeReference import org.springframework.http.HttpEntity @@ -183,7 +184,8 @@ class TestRestTemplateExtensionsTests { val var1 = "var1" val var2 = "var2" template.exchange>(url, method, entity, var1, var2) - verify(template, times(1)).exchange(url, method, entity, object : ParameterizedTypeReference>() {}, var1, var2) + verify(template, times(1)).exchange(url, method, entity, + object : ParameterizedTypeReference>() {}, var1, var2) } @Test @@ -193,7 +195,8 @@ class TestRestTemplateExtensionsTests { val entity = mock>() val vars = mapOf(Pair("key1", "value1"), Pair("key2", "value2")) template.exchange>(url, method, entity, vars) - verify(template, times(1)).exchange(url, method, entity, object : ParameterizedTypeReference>() {}, vars) + verify(template, times(1)).exchange(url, method, entity, + object : ParameterizedTypeReference>() {}, vars) } @Test @@ -202,26 +205,32 @@ class TestRestTemplateExtensionsTests { val method = HttpMethod.GET val entity = mock>() template.exchange>(url, method, entity) - verify(template, times(1)).exchange(url, method, entity, object : ParameterizedTypeReference>() {}) + verify(template, times(1)).exchange(url, method, entity, + object : ParameterizedTypeReference>() {}) } @Test fun `exchange with reified type parameters, String, HttpEntity`() { val entity = mock>() template.exchange>(entity) - verify(template, times(1)).exchange(entity, object : ParameterizedTypeReference>() {}) + verify(template, times(1)).exchange(entity, + object : ParameterizedTypeReference>() {}) } @Test 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 -> arrayOf(ParameterizedTypeReference::class, Class::class).forEach { kClass -> if (method.parameterTypes.contains(kClass.java)) { - val parameters = mutableListOf>(TestRestTemplate::class.java).apply { addAll(method.parameterTypes.filter { it != kClass.java }) } - val f = extensions.getDeclaredMethod(method.name, *parameters.toTypedArray()).kotlinFunction!! + val parameters = mutableListOf>(TestRestTemplate::class.java) + .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(listOf(Any::class.createType()), f.typeParameters[0].upperBounds) + Assert.assertEquals(listOf(Any::class.createType()), + f.typeParameters[0].upperBounds) } } }