-
Notifications
You must be signed in to change notification settings - Fork 40.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Introduce TestRestTemplate Kotlin extensions #11039
Closed
sdeleuze
wants to merge
1
commit into
spring-projects:master
from
sdeleuze:test-rest-template-extensions
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
234 changes: 234 additions & 0 deletions
234
...st/src/main/kotlin/org/springframework/boot/test/web/client/TestRestTemplateExtensions.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,234 @@ | ||
/* | ||
* Copyright 2012-2017 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.boot.test.web.client | ||
|
||
import org.springframework.core.ParameterizedTypeReference | ||
import org.springframework.http.HttpEntity | ||
import org.springframework.http.HttpMethod | ||
import org.springframework.http.RequestEntity | ||
import org.springframework.http.ResponseEntity | ||
import org.springframework.web.client.RestClientException | ||
import java.net.URI | ||
|
||
/** | ||
* Extension for [TestRestTemplate.getForObject] avoiding specifying the type | ||
* parameter thanks to Kotlin reified type parameters. | ||
* | ||
* @author Sebastien Deleuze | ||
* @since 2.0.0 | ||
*/ | ||
@Throws(RestClientException::class) | ||
inline fun <reified T: Any> TestRestTemplate.getForObject(url: String, vararg uriVariables: Any): T? = | ||
getForObject(url, T::class.java, *uriVariables) | ||
|
||
/** | ||
* Extension for [TestRestTemplate.getForObject] avoiding specifying the type | ||
* parameter thanks to Kotlin reified type parameters. | ||
* | ||
* @author Sebastien Deleuze | ||
* @since 2.0.0 | ||
*/ | ||
@Throws(RestClientException::class) | ||
inline fun <reified T: Any> TestRestTemplate.getForObject(url: String, uriVariables: Map<String, Any?>): T? = | ||
getForObject(url, T::class.java, uriVariables) | ||
|
||
/** | ||
* Extension for [TestRestTemplate.getForObject] avoiding specifying the type parameter | ||
* thanks to Kotlin reified type parameters. | ||
* | ||
* @author Sebastien Deleuze | ||
* @since 2.0.0 | ||
*/ | ||
@Throws(RestClientException::class) | ||
inline fun <reified T: Any> TestRestTemplate.getForObject(url: URI): T? = | ||
getForObject(url, T::class.java) | ||
|
||
/** | ||
* Extension for [TestRestTemplate.getForEntity] avoiding requiring the type parameter | ||
* thanks to Kotlin reified type parameters. | ||
* | ||
* @author Sebastien Deleuze | ||
* @since 2.0.0 | ||
*/ | ||
@Throws(RestClientException::class) | ||
inline fun <reified T: Any> TestRestTemplate.getForEntity(url: URI): ResponseEntity<T> = | ||
getForEntity(url, T::class.java) | ||
|
||
/** | ||
* Extension for [TestRestTemplate.getForEntity] avoiding requiring the type parameter | ||
* thanks to Kotlin reified type parameters. | ||
* | ||
* @author Sebastien Deleuze | ||
* @since 2.0.0 | ||
*/ | ||
@Throws(RestClientException::class) | ||
inline fun <reified T: Any> TestRestTemplate.getForEntity(url: String, vararg uriVariables: Any): ResponseEntity<T> = | ||
getForEntity(url, T::class.java, *uriVariables) | ||
|
||
/** | ||
* Extension for [TestRestTemplate.getForEntity] avoiding requiring the type parameter | ||
* thanks to Kotlin reified type parameters. | ||
* | ||
* @author Sebastien Deleuze | ||
* @since 2.0.0 | ||
*/ | ||
@Throws(RestClientException::class) | ||
inline fun <reified T: Any> TestRestTemplate.getForEntity(url: String, uriVariables: Map<String, *>): ResponseEntity<T> = | ||
getForEntity(url, T::class.java, uriVariables) | ||
|
||
/** | ||
* Extension for [TestRestTemplate.patchForObject] avoiding specifying the type parameter | ||
* thanks to Kotlin reified type parameters. | ||
* | ||
* @author Sebastien Deleuze | ||
* @since 2.0.0 | ||
*/ | ||
@Throws(RestClientException::class) | ||
inline fun <reified T: Any> TestRestTemplate.patchForObject(url: String, request: Any, vararg uriVariables: Any): T? = | ||
patchForObject(url, request, T::class.java, *uriVariables) | ||
|
||
/** | ||
* Extension for [TestRestTemplate.patchForObject] avoiding specifying the type parameter | ||
* thanks to Kotlin reified type parameters. | ||
* | ||
* @author Sebastien Deleuze | ||
* @since 2.0.0 | ||
*/ | ||
@Throws(RestClientException::class) | ||
inline fun <reified T: Any> TestRestTemplate.patchForObject(url: String, request: Any, uriVariables: Map<String, *>): T? = | ||
patchForObject(url, request, T::class.java, uriVariables) | ||
|
||
/** | ||
* Extension for [TestRestTemplate.patchForObject] avoiding specifying the type parameter | ||
* thanks to Kotlin reified type parameters. | ||
* | ||
* @author Sebastien Deleuze | ||
* @since 2.0.0 | ||
*/ | ||
@Throws(RestClientException::class) | ||
inline fun <reified T: Any> TestRestTemplate.patchForObject(url: URI, request: Any): T? = | ||
patchForObject(url, request, T::class.java) | ||
|
||
/** | ||
* Extension for [TestRestTemplate.postForObject] avoiding specifying the type parameter | ||
* thanks to Kotlin reified type parameters. | ||
* | ||
* @author Sebastien Deleuze | ||
* @since 2.0.0 | ||
*/ | ||
@Throws(RestClientException::class) | ||
inline fun <reified T: Any> TestRestTemplate.postForObject(url: String, request: Any, vararg uriVariables: Any): T? = | ||
postForObject(url, request, T::class.java, *uriVariables) | ||
|
||
/** | ||
* Extension for [TestRestTemplate.postForObject] avoiding specifying the type parameter | ||
* thanks to Kotlin reified type parameters. | ||
* | ||
* @author Sebastien Deleuze | ||
* @since 2.0.0 | ||
*/ | ||
@Throws(RestClientException::class) | ||
inline fun <reified T: Any> TestRestTemplate.postForObject(url: String, request: Any, uriVariables: Map<String, *>): T? = | ||
postForObject(url, request, T::class.java, uriVariables) | ||
|
||
/** | ||
* Extension for [TestRestTemplate.postForObject] avoiding specifying the type parameter | ||
* thanks to Kotlin reified type parameters. | ||
* | ||
* @author Sebastien Deleuze | ||
* @since 2.0.0 | ||
*/ | ||
@Throws(RestClientException::class) | ||
inline fun <reified T: Any> TestRestTemplate.postForObject(url: URI, request: Any): T? = | ||
postForObject(url, request, T::class.java) | ||
|
||
/** | ||
* Extension for [TestRestTemplate.postForEntity] avoiding specifying the type parameter | ||
* thanks to Kotlin reified type parameters. | ||
* | ||
* @author Sebastien Deleuze | ||
* @since 2.0.0 | ||
*/ | ||
@Throws(RestClientException::class) | ||
inline fun <reified T: Any> TestRestTemplate.postForEntity(url: String, request: Any, vararg uriVariables: Any): ResponseEntity<T> = | ||
postForEntity(url, request, T::class.java, *uriVariables) | ||
|
||
/** | ||
* Extension for [TestRestTemplate.postForEntity] avoiding specifying the type parameter | ||
* thanks to Kotlin reified type parameters. | ||
* | ||
* @author Sebastien Deleuze | ||
* @since 2.0.0 | ||
*/ | ||
@Throws(RestClientException::class) | ||
inline fun <reified T: Any> TestRestTemplate.postForEntity(url: String, request: Any, uriVariables: Map<String, *>): ResponseEntity<T> = | ||
postForEntity(url, request, T::class.java, uriVariables) | ||
|
||
/** | ||
* Extension for [TestRestTemplate.postForEntity] avoiding specifying the type parameter | ||
* thanks to Kotlin reified type parameters. | ||
* | ||
* @author Sebastien Deleuze | ||
* @since 2.0.0 | ||
*/ | ||
@Throws(RestClientException::class) | ||
inline fun <reified T: Any> TestRestTemplate.postForEntity(url: URI, request: Any): ResponseEntity<T> = | ||
postForEntity(url, request, T::class.java) | ||
|
||
/** | ||
* Extension for [TestRestTemplate.exchange] avoiding specifying the type parameter | ||
* thanks to Kotlin reified type parameters. | ||
* | ||
* @author Sebastien Deleuze | ||
* @since 2.0.0 | ||
*/ | ||
@Throws(RestClientException::class) | ||
inline fun <reified T: Any> TestRestTemplate.exchange(url: String, method: HttpMethod, requestEntity: HttpEntity<*>, vararg uriVariables: Any): ResponseEntity<T> = | ||
exchange(url, method, requestEntity, object : ParameterizedTypeReference<T>() {}, *uriVariables) | ||
|
||
/** | ||
* Extension for [TestRestTemplate.exchange] avoiding specifying the type parameter | ||
* thanks to Kotlin reified type parameters. | ||
* | ||
* @author Sebastien Deleuze | ||
* @since 2.0.0 | ||
*/ | ||
@Throws(RestClientException::class) | ||
inline fun <reified T: Any> TestRestTemplate.exchange(url: String, method: HttpMethod, requestEntity: HttpEntity<*>, uriVariables: Map<String, *>): ResponseEntity<T> = | ||
exchange(url, method, requestEntity, object : ParameterizedTypeReference<T>() {}, uriVariables) | ||
|
||
/** | ||
* Extension for [TestRestTemplate.exchange] avoiding specifying the type parameter | ||
* thanks to Kotlin reified type parameters. | ||
* | ||
* @author Sebastien Deleuze | ||
* @since 2.0.0 | ||
*/ | ||
@Throws(RestClientException::class) | ||
inline fun <reified T: Any> TestRestTemplate.exchange(url: URI, method: HttpMethod, requestEntity: HttpEntity<*>): ResponseEntity<T> = | ||
exchange(url, method, requestEntity, object : ParameterizedTypeReference<T>() {}) | ||
|
||
/** | ||
* Extension for [TestRestTemplate.exchange] avoiding specifying the type parameter | ||
* thanks to Kotlin reified type parameters. | ||
* | ||
* @author Sebastien Deleuze | ||
* @since 2.0.0 | ||
*/ | ||
@Throws(RestClientException::class) | ||
inline fun <reified T: Any> TestRestTemplate.exchange(requestEntity: RequestEntity<*>): ResponseEntity<T> = | ||
exchange(requestEntity, object : ParameterizedTypeReference<T>() {}) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am confused. In the
spring-boot
module you've set the maven compiler phases tonone
. Why isn't it necessary here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch, it is, I will update the PR accordingly.