私がそれを機能させる方法は、残りのURIの一部としてコンテキストパスを含めることと、RequestBuilderでcontextPathを呼び出すことです。これが実際の例です。私の例では、「api」がコンテキストルートでした。それをリクエストに含め、RequestBuilder で contextPath メソッドを呼び出して設定する必要がありました。
@Andy Wilkinson:これはあなたが言ったことを繰り返しているかもしれませんが、私のような人にとっては完全に機能する例がより明確になると思いました:)
@RunWith(MockitoJUnitRunner.class)
public class UserControllerTest {
private UserController controller;
private MockMvc mockMvc;
@Rule
public RestDocumentation restDocumentation = new RestDocumentation("target/generated-snippets");
@Before
public void setUp() {
controller = new UserController();
mockMvc = standaloneSetup(controller).apply(documentationConfiguration(this.restDocumentation)).build();
}
@Test
public void testUsers() throws Exception {
this.mockMvc.perform(get("/api/users/{userId}","1234").contextPath("/api"))
.andExpect(status().isOk())
.andDo(document("user-information", pathParameters(
parameterWithName("userId").description("user id.")
), responseFields(
fieldWithPath("firstName").description("first name of the user. "),
fieldWithPath("lastName").description("Last name of the user. ")
)))
.andDo(print());
}
}