pathParameters documentation exception (urlTemplate not found)
Asked Answered
P

1

39

When using pathParameters to document the URI path parameters like below

@Test
public void documentGetRouteById() throws Exception {
    this.mockMvc.perform(get("/route/{id}", "FooBar")).andExpect(status().isOk())
            .andDo(document("api-getRouteById",
                    pathParameters(parameterWithName("id").description("die Routen ID"))));
}

I get the following excpetion

java.lang.IllegalArgumentException: urlTemplate not found. Did you use RestDocumentationRequestBuilders to build the request?
    at org.springframework.util.Assert.notNull(Assert.java:112)
    at org.springframework.restdocs.request.PathParametersSnippet.extractUrlTemplate(PathParametersSnippet.java:95)
    at org.springframework.restdocs.request.PathParametersSnippet.extractActualParameters(PathParametersSnippet.java:82)
    at org.springframework.restdocs.request.AbstractParametersSnippet.verifyParameterDescriptors(AbstractParametersSnippet.java:77)
    at org.springframework.restdocs.request.AbstractParametersSnippet.createModel(AbstractParametersSnippet.java:65)
    at org.springframework.restdocs.request.PathParametersSnippet.createModel(PathParametersSnippet.java:67)
    at org.springframework.restdocs.snippet.TemplatedSnippet.document(TemplatedSnippet.java:64)
    at org.springframework.restdocs.mockmvc.RestDocumentationResultHandler.handle(RestDocumentationResultHandler.java:101)
    at org.springframework.test.web.servlet.MockMvc$1.andDo(MockMvc.java:158)

I am pretty sure I did the test setup like explained here.

What could I probably have done wrong?

(Spring REST docs version is 1.0.0.BUILD-SNAPSHOT)

Papotto answered 1/10, 2015 at 13:38 Comment(0)
D
66

The exception message is trying to point you in the right direction:

urlTemplate not found. Did you use RestDocumentationRequestBuilders to build the request?

You need to use RestDocumentationRequestBuilders so that Spring REST Docs can capture the URL and extract the parameters from it. This is mentioned in the documentation where it says:

To make the path parameters available for documentation, the request must be built using one of the methods on RestDocumentationRequestBuilders rather than MockMvcRequestBuilders.

Replacing your static import of MockMvcRequestBuilders.get with one for RestDocumentationRequestBuilders.get should resolve the problem.

Downwind answered 1/10, 2015 at 13:52 Comment(1)
So you say it is all written down? You're right! Well documented module. The problem was once again in front of the monitor.Papotto

© 2022 - 2024 — McMap. All rights reserved.