Add Kotlin extensions for WebTestClient API
Issue: SPR-15622
This commit is contained in:
@@ -1039,6 +1039,7 @@ project("spring-test") {
|
||||
optional("org.reactivestreams:reactive-streams")
|
||||
optional("io.projectreactor:reactor-core")
|
||||
optional("io.projectreactor:reactor-test")
|
||||
optional("org.jetbrains.kotlin:kotlin-stdlib:${kotlinVersion}")
|
||||
testCompile(project(":spring-context-support"))
|
||||
testCompile(project(":spring-oxm"))
|
||||
testCompile("javax.mail:javax.mail-api:${javamailVersion}")
|
||||
|
||||
@@ -536,7 +536,7 @@ public interface WebTestClient {
|
||||
*/
|
||||
<B> BodySpec<B, ?> expectBody(ParameterizedTypeReference<B> bodyType);
|
||||
|
||||
/**
|
||||
/**
|
||||
* Declare expectations on the response body decoded to {@code List<E>}.
|
||||
* @param elementType the expected List element type
|
||||
*/
|
||||
|
||||
@@ -0,0 +1,84 @@
|
||||
/*
|
||||
* Copyright 2002-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.test.web.reactive.server
|
||||
|
||||
import org.reactivestreams.Publisher
|
||||
import kotlin.reflect.KClass
|
||||
|
||||
/**
|
||||
* Extension for [WebTestClient.RequestBodySpec.body] providing a variant without explicit class
|
||||
* parameter thanks to Kotlin reified type parameters.
|
||||
*
|
||||
* @author Sebastien Deleuze
|
||||
* @since 5.0
|
||||
*/
|
||||
inline fun <reified T : Any, S : Publisher<T>> WebTestClient.RequestBodySpec.body(publisher: S): WebTestClient.RequestHeadersSpec<*>
|
||||
= body(publisher, T::class.java)
|
||||
|
||||
/**
|
||||
* Extension for [WebTestClient.ResponseSpec.expectBody] providing a [KClass] based variant.
|
||||
*
|
||||
* @author Sebastien Deleuze
|
||||
* @since 5.0
|
||||
*/
|
||||
fun <B : Any> WebTestClient.ResponseSpec.expectBody(type: KClass<B>): WebTestClient.BodySpec<B, *> =
|
||||
expectBody(type.java)
|
||||
|
||||
/**
|
||||
* Extension for [WebTestClient.ResponseSpec.expectBody] providing a `expectBody<Foo>()` variant.
|
||||
*
|
||||
* @author Sebastien Deleuze
|
||||
* @since 5.0
|
||||
*/
|
||||
inline fun <reified B : Any> WebTestClient.ResponseSpec.expectBody(): WebTestClient.BodySpec<B, *> =
|
||||
expectBody(B::class.java)
|
||||
|
||||
/**
|
||||
* Extension for [WebTestClient.ResponseSpec.expectBodyList] providing a [KClass] based variant.
|
||||
*
|
||||
* @author Sebastien Deleuze
|
||||
* @since 5.0
|
||||
*/
|
||||
fun <E : Any> WebTestClient.ResponseSpec.expectBodyList(type: KClass<E>): WebTestClient.ListBodySpec<E> =
|
||||
expectBodyList(type.java)
|
||||
|
||||
/**
|
||||
* Extension for [WebTestClient.ResponseSpec.expectBodyList] providing a `expectBodyList<Foo>()` variant.
|
||||
*
|
||||
* @author Sebastien Deleuze
|
||||
* @since 5.0
|
||||
*/
|
||||
inline fun <reified E : Any> WebTestClient.ResponseSpec.expectBodyList(): WebTestClient.ListBodySpec<E> =
|
||||
expectBodyList(E::class.java)
|
||||
|
||||
/**
|
||||
* Extension for [WebTestClient.ResponseSpec.returnResult] providing a [KClass] based variant.
|
||||
*
|
||||
* @author Sebastien Deleuze
|
||||
* @since 5.0
|
||||
*/
|
||||
fun <T : Any> WebTestClient.ResponseSpec.returnResult(type: KClass<T>): FluxExchangeResult<T> =
|
||||
returnResult(type.java)
|
||||
|
||||
/**
|
||||
* Extension for [WebTestClient.ResponseSpec.returnResult] providing a `returnResult<Foo>()` variant.
|
||||
*
|
||||
* @author Sebastien Deleuze
|
||||
* @since 5.0
|
||||
*/
|
||||
inline fun <reified T : Any> WebTestClient.ResponseSpec.returnResult(): FluxExchangeResult<T> =
|
||||
returnResult(T::class.java)
|
||||
@@ -0,0 +1,74 @@
|
||||
package org.springframework.test.web.reactive.server
|
||||
|
||||
import com.nhaarman.mockito_kotlin.mock
|
||||
import com.nhaarman.mockito_kotlin.times
|
||||
import com.nhaarman.mockito_kotlin.verify
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.mockito.Answers
|
||||
import org.mockito.Mock
|
||||
import org.mockito.junit.MockitoJUnitRunner
|
||||
import org.reactivestreams.Publisher
|
||||
|
||||
|
||||
/**
|
||||
* Mock object based tests for [WebTestClient] Kotlin extensions
|
||||
*
|
||||
* @author Sebastien Deleuze
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner::class)
|
||||
class WebTestClientExtensionsTests {
|
||||
|
||||
@Mock(answer = Answers.RETURNS_MOCKS)
|
||||
lateinit var requestBodySpec: WebTestClient.RequestBodySpec
|
||||
|
||||
@Mock(answer = Answers.RETURNS_MOCKS)
|
||||
lateinit var responseSpec: WebTestClient.ResponseSpec
|
||||
|
||||
|
||||
@Test
|
||||
fun `RequestBodySpec#body with Publisher and reified type parameters`() {
|
||||
val body = mock<Publisher<Foo>>()
|
||||
requestBodySpec.body(body)
|
||||
verify(requestBodySpec, times(1)).body(body, Foo::class.java)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `ResponseSpec#expectBody with KClass`() {
|
||||
responseSpec.expectBody(Foo::class)
|
||||
verify(responseSpec, times(1)).expectBody(Foo::class.java)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `ResponseSpec#expectBody with reified type parameters`() {
|
||||
responseSpec.expectBody<Foo>()
|
||||
verify(responseSpec, times(1)).expectBody(Foo::class.java)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `ResponseSpec#expectBodyList with KClass`() {
|
||||
responseSpec.expectBodyList(Foo::class)
|
||||
verify(responseSpec, times(1)).expectBodyList(Foo::class.java)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `ResponseSpec#expectBodyList with reified type parameters`() {
|
||||
responseSpec.expectBodyList<Foo>()
|
||||
verify(responseSpec, times(1)).expectBodyList(Foo::class.java)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `ResponseSpec#returnResult with KClass`() {
|
||||
responseSpec.returnResult(Foo::class)
|
||||
verify(responseSpec, times(1)).returnResult(Foo::class.java)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `ResponseSpec#returnResult with reified type parameters`() {
|
||||
responseSpec.returnResult<Foo>()
|
||||
verify(responseSpec, times(1)).returnResult(Foo::class.java)
|
||||
}
|
||||
|
||||
class Foo
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user