Skip to content
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

When i use webtestclient, i am getting on error message 'please use RestDocumentationRequestBuilders with urlTemplate to construct the request' #234

Open
JoeyCLO opened this issue Apr 4, 2023 · 1 comment

Comments

@JoeyCLO
Copy link

JoeyCLO commented Apr 4, 2023

RestDocumentationRequestBuilders class is only use mockmvc. isn't it? However, when using the webtestclient, you must also use the RestDocumentationRequestBuilders.

  • build.gradle.kts dependencies
        dependencies {
            // 2.0.6.RELEASE
            testImplementation("org.springframework.restdocs:spring-restdocs-core")
            testImplementation("org.springframework.restdocs:spring-restdocs-webtestclient")
            // 0.16.4
            testImplementation("com.epages:restdocs-api-spec:${project.extra["version.restdocsApiSpec"]}")
            testImplementation("com.epages:restdocs-api-spec-webtestclient:${project.extra["version.restdocsApiSpec"]}")
        }
  • webtestclient usage
client.get()
          .uri {
              it.path(testPath)
                  .queryParam("styles", styles)
                  .build()
          }
          .accept(MediaType.APPLICATION_JSON)
          .exchange()
          .expectStatus().isOk
          .expectBody(String::class.java)
          .consumeWith (
              document("test try on", "get result", true,
                  requestParameters(
                      parameterWithName("styles").description("Sequence")
                  ),
                  responseHeaders(
                      headerWithName(HttpHeaders.CONTENT_TYPE).description(MediaType.APPLICATION_JSON),
                  ),
                  responseFields(
                      fieldWithPath("estimatedTime").type(JsonFieldType.NUMBER).description("Estimated time"),
                      fieldWithPath("estimatedCount").type(JsonFieldType.NUMBER).description("Estimated count"),
                      fieldWithPath("requestTotalCount").type(JsonFieldType.NUMBER).description("Request total count"),
                  )
              )
          )
  • Error message
Missing URL template - please use RestDocumentationRequestBuilders with urlTemplate to construct the request
com.epages.restdocs.apispec.ResourceSnippet$MissingUrlTemplateException: Missing URL template - please use RestDocumentationRequestBuilders with urlTemplate to construct the request
	at com.epages.restdocs.apispec.ResourceSnippet$getUriComponents$2.get(ResourceSnippet.kt:104)
	at com.epages.restdocs.apispec.ResourceSnippet$getUriComponents$2.get(ResourceSnippet.kt:21)
	at java.base/java.util.Optional.orElseThrow(Optional.java:403)
	at com.epages.restdocs.apispec.ResourceSnippet.getUriComponents(ResourceSnippet.kt:104)
	at com.epages.restdocs.apispec.ResourceSnippet.createModel(ResourceSnippet.kt:56)
	at com.epages.restdocs.apispec.ResourceSnippet.document(ResourceSnippet.kt:34)
	at org.springframework.restdocs.generate.RestDocumentationGenerator.handle(RestDocumentationGenerator.java:191)
	at org.springframework.restdocs.webtestclient.WebTestClientRestDocumentation.lambda$document$0(WebTestClientRestDocumentation.java:77)
	at org.springframework.test.web.reactive.server.DefaultWebTestClient$DefaultBodySpec.lambda$consumeWith$4(DefaultWebTestClient.java:582)
	at org.springframework.test.web.reactive.server.ExchangeResult.assertWithDiagnostics(ExchangeResult.java:231)
	at org.springframework.test.web.reactive.server.DefaultWebTestClient$DefaultBodySpec.consumeWith(DefaultWebTestClient.java:582)

Is there any way to solve this problem?

@reneschroeder0000
Copy link

You can use this instead (using the String version of the uri function):

client.get().uri(
	UriComponentsBuilder
		.fromPath(testPath)
		.queryParam("styles", styles)
		.toUriString()
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants