When using uriFunction to construct URIs using the UriBuilder the URL Template is not captured.
Test
package com.example.restdocs.demo
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.test.web.reactive.server.WebTestClient
@SpringBootTest
@AutoConfigureWebTestClient
internal class DemoControllerTest(
@Autowired private val webTestClient: WebTestClient
) {
@Test
fun `should return uriTemplate from UriBuilder`() {
webTestClient.get().uri { it.path("/demo").build() }
.exchange().expectBody()
.consumeWith { assertThat(it.uriTemplate).isEqualTo("/demo?foo=bar") }
}
@Test
fun `should return uriTemplate from URI`() {
webTestClient.get().uri("/demo")
.exchange().expectBody().consumeWith { assertThat(it.uriTemplate).isEqualTo("/demo?foo=bar") }
}
}
build.gradle.kts
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
id("org.springframework.boot") version "2.7.2"
id("io.spring.dependency-management") version "1.0.12.RELEASE"
id("org.asciidoctor.convert") version "1.5.8"
kotlin("jvm") version "1.6.21"
kotlin("plugin.spring") version "1.6.21"
}
group = "com.example.restdocs"
version = "0.0.1-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_17
repositories {
mavenCentral()
}
val snippetsDir = file("build/generated-snippets")
dependencies {
implementation("org.springframework.boot:spring-boot-starter-webflux")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
implementation("io.projectreactor.kotlin:reactor-kotlin-extensions")
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-reactor")
testImplementation("org.springframework.boot:spring-boot-starter-test")
testImplementation("io.projectreactor:reactor-test")
testImplementation("org.springframework.restdocs:spring-restdocs-webtestclient")
}
tasks.withType<KotlinCompile> {
kotlinOptions {
freeCompilerArgs = listOf("-Xjsr305=strict")
jvmTarget = "17"
}
}
val test = tasks.withType<Test> {
useJUnitPlatform()
}
tasks.test {
outputs.dir(snippetsDir)
}
tasks.asciidoctor {
inputs.dir(snippetsDir)
dependsOn(test)
}
Comment From: wilkinsona
A template isn't created when using the UriBuilder. You should use one of the methods on WebTestClient.UriSpec that state in their javadoc that they work with a template instead. Also, please note that WebTestClient and WebClient are part of Spring Framework which is managed as a separate project.