2015-10-01 25 views
11

Khi sử dụng pathParameters để ghi lại các thông số đường URI như dưới đâypathParameters ngoại lệ tài liệu (urlTemplate không tìm thấy)

@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")))); 
} 

tôi nhận được excpetion sau

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) 

Tôi khá chắc chắn tôi đã thiết lập thử nghiệm như được giải thích here.

Tôi có thể đã làm gì sai?

(Spring REST của tài liệu phiên bản là 1.0.0.BUILD-SNAPSHOT)

Trả lời

27

Thông điệp ngoại lệ đang cố gắng chỉ cho bạn đi đúng hướng: không tìm thấy

urlTemplate. Bạn có sử dụng RestDocumentationRequestBuilders để xây dựng yêu cầu không?

Bạn cần sử dụng để Spring REST Documents có thể nắm bắt URL và trích xuất các tham số từ đó. Đây là mentioned in the documentation nơi nó nói:

Để thực hiện các thông số con đường có sẵn cho tài liệu hướng dẫn, yêu cầu phải được xây dựng bằng một trong những phương pháp trên RestDocumentationRequestBuilders hơn MockMvcRequestBuilders.

Thay thế nhập tĩnh của bạn MockMvcRequestBuilders.get bằng một cho RestDocumentationRequestBuilders.get sẽ giải quyết được sự cố.

+0

Vì vậy, bạn nói rằng tất cả được viết ra? Bạn đúng! Mô-đun tài liệu tốt. Vấn đề lại một lần nữa ở phía trước màn hình. – FrVaBe

Các vấn đề liên quan