Skip to content

Commit 3373a3f

Browse files
committed
Add Kotlin extensions for WebTestClient API
Issue: SPR-15622
1 parent a665da4 commit 3373a3f

File tree

4 files changed

+160
-1
lines changed

4 files changed

+160
-1
lines changed

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1039,6 +1039,7 @@ project("spring-test") {
10391039
optional("org.reactivestreams:reactive-streams")
10401040
optional("io.projectreactor:reactor-core")
10411041
optional("io.projectreactor:reactor-test")
1042+
optional("org.jetbrains.kotlin:kotlin-stdlib:${kotlinVersion}")
10421043
testCompile(project(":spring-context-support"))
10431044
testCompile(project(":spring-oxm"))
10441045
testCompile("javax.mail:javax.mail-api:${javamailVersion}")

spring-test/src/main/java/org/springframework/test/web/reactive/server/WebTestClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,7 @@ interface ResponseSpec {
536536
*/
537537
<B> BodySpec<B, ?> expectBody(ParameterizedTypeReference<B> bodyType);
538538

539-
/**
539+
/**
540540
* Declare expectations on the response body decoded to {@code List<E>}.
541541
* @param elementType the expected List element type
542542
*/
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Copyright 2002-2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.test.web.reactive.server
18+
19+
import org.reactivestreams.Publisher
20+
import kotlin.reflect.KClass
21+
22+
/**
23+
* Extension for [WebTestClient.RequestBodySpec.body] providing a variant without explicit class
24+
* parameter thanks to Kotlin reified type parameters.
25+
*
26+
* @author Sebastien Deleuze
27+
* @since 5.0
28+
*/
29+
inline fun <reified T : Any, S : Publisher<T>> WebTestClient.RequestBodySpec.body(publisher: S): WebTestClient.RequestHeadersSpec<*>
30+
= body(publisher, T::class.java)
31+
32+
/**
33+
* Extension for [WebTestClient.ResponseSpec.expectBody] providing a [KClass] based variant.
34+
*
35+
* @author Sebastien Deleuze
36+
* @since 5.0
37+
*/
38+
fun <B : Any> WebTestClient.ResponseSpec.expectBody(type: KClass<B>): WebTestClient.BodySpec<B, *> =
39+
expectBody(type.java)
40+
41+
/**
42+
* Extension for [WebTestClient.ResponseSpec.expectBody] providing a `expectBody<Foo>()` variant.
43+
*
44+
* @author Sebastien Deleuze
45+
* @since 5.0
46+
*/
47+
inline fun <reified B : Any> WebTestClient.ResponseSpec.expectBody(): WebTestClient.BodySpec<B, *> =
48+
expectBody(B::class.java)
49+
50+
/**
51+
* Extension for [WebTestClient.ResponseSpec.expectBodyList] providing a [KClass] based variant.
52+
*
53+
* @author Sebastien Deleuze
54+
* @since 5.0
55+
*/
56+
fun <E : Any> WebTestClient.ResponseSpec.expectBodyList(type: KClass<E>): WebTestClient.ListBodySpec<E> =
57+
expectBodyList(type.java)
58+
59+
/**
60+
* Extension for [WebTestClient.ResponseSpec.expectBodyList] providing a `expectBodyList<Foo>()` variant.
61+
*
62+
* @author Sebastien Deleuze
63+
* @since 5.0
64+
*/
65+
inline fun <reified E : Any> WebTestClient.ResponseSpec.expectBodyList(): WebTestClient.ListBodySpec<E> =
66+
expectBodyList(E::class.java)
67+
68+
/**
69+
* Extension for [WebTestClient.ResponseSpec.returnResult] providing a [KClass] based variant.
70+
*
71+
* @author Sebastien Deleuze
72+
* @since 5.0
73+
*/
74+
fun <T : Any> WebTestClient.ResponseSpec.returnResult(type: KClass<T>): FluxExchangeResult<T> =
75+
returnResult(type.java)
76+
77+
/**
78+
* Extension for [WebTestClient.ResponseSpec.returnResult] providing a `returnResult<Foo>()` variant.
79+
*
80+
* @author Sebastien Deleuze
81+
* @since 5.0
82+
*/
83+
inline fun <reified T : Any> WebTestClient.ResponseSpec.returnResult(): FluxExchangeResult<T> =
84+
returnResult(T::class.java)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package org.springframework.test.web.reactive.server
2+
3+
import com.nhaarman.mockito_kotlin.mock
4+
import com.nhaarman.mockito_kotlin.times
5+
import com.nhaarman.mockito_kotlin.verify
6+
import org.junit.Test
7+
import org.junit.runner.RunWith
8+
import org.mockito.Answers
9+
import org.mockito.Mock
10+
import org.mockito.junit.MockitoJUnitRunner
11+
import org.reactivestreams.Publisher
12+
13+
14+
/**
15+
* Mock object based tests for [WebTestClient] Kotlin extensions
16+
*
17+
* @author Sebastien Deleuze
18+
*/
19+
@RunWith(MockitoJUnitRunner::class)
20+
class WebTestClientExtensionsTests {
21+
22+
@Mock(answer = Answers.RETURNS_MOCKS)
23+
lateinit var requestBodySpec: WebTestClient.RequestBodySpec
24+
25+
@Mock(answer = Answers.RETURNS_MOCKS)
26+
lateinit var responseSpec: WebTestClient.ResponseSpec
27+
28+
29+
@Test
30+
fun `RequestBodySpec#body with Publisher and reified type parameters`() {
31+
val body = mock<Publisher<Foo>>()
32+
requestBodySpec.body(body)
33+
verify(requestBodySpec, times(1)).body(body, Foo::class.java)
34+
}
35+
36+
@Test
37+
fun `ResponseSpec#expectBody with KClass`() {
38+
responseSpec.expectBody(Foo::class)
39+
verify(responseSpec, times(1)).expectBody(Foo::class.java)
40+
}
41+
42+
@Test
43+
fun `ResponseSpec#expectBody with reified type parameters`() {
44+
responseSpec.expectBody<Foo>()
45+
verify(responseSpec, times(1)).expectBody(Foo::class.java)
46+
}
47+
48+
@Test
49+
fun `ResponseSpec#expectBodyList with KClass`() {
50+
responseSpec.expectBodyList(Foo::class)
51+
verify(responseSpec, times(1)).expectBodyList(Foo::class.java)
52+
}
53+
54+
@Test
55+
fun `ResponseSpec#expectBodyList with reified type parameters`() {
56+
responseSpec.expectBodyList<Foo>()
57+
verify(responseSpec, times(1)).expectBodyList(Foo::class.java)
58+
}
59+
60+
@Test
61+
fun `ResponseSpec#returnResult with KClass`() {
62+
responseSpec.returnResult(Foo::class)
63+
verify(responseSpec, times(1)).returnResult(Foo::class.java)
64+
}
65+
66+
@Test
67+
fun `ResponseSpec#returnResult with reified type parameters`() {
68+
responseSpec.returnResult<Foo>()
69+
verify(responseSpec, times(1)).returnResult(Foo::class.java)
70+
}
71+
72+
class Foo
73+
74+
}

0 commit comments

Comments
 (0)