現在のバージョンの spring-restdoc: 1.1.2.RELEASE を使用しています。
ヘッダーに Cookie が必要な API を作成しました。これが私のコントローラーです:
@RequestMapping(value = "/{username}/userInfo", produces = {APPLICATION_JSON_UTF8_VALUE}, method = RequestMethod.GET)
public ResponseEntity<UserInfo> getUserInfo(@CookieValue("identity") String cookie,
@PathVariable(value = "username") String username) {
LOGGER.info("[getUserInfo] user = {}", username);
ResponseEntity<UserInfo> responseEntity = new ResponseEntity<>(userService.findUserInfos(cookie, username), HttpStatus.OK);
LOGGER.debug("[getUserInfo] response = {}", responseEntity.toString());
return responseEntity;
}
getUserInfos のドキュメントのテストを書いた後。これが私のテストです:
@Test
public void getUserInfoOK() throws Exception {
when(userService.findUserInfos(cookieCaptor.capture(), userNameCaptor.capture())).thenReturn(
createUserInfo());
Cookie cookie = new Cookie("identity", "cookieForTest");
UriBuilder builder = UriBuilder.fromPath("").path("/v1/users/{username}/userInfo");
URI uri = builder.build("userTest");
MockHttpServletRequestBuilder request = RestDocumentationRequestBuilders.get("http://localhost:8070" + "/api/user" + "/v1/users/{username}/userInfo", "userTest")
.header("Host", "localhost")
.cookie(cookie)
.accept(MediaType.APPLICATION_JSON_UTF8)
.servletPath(uri.toString()).contextPath("/api/user");
mockMvc.perform(request)
.andDo(print())
.andDo(document("{method-name}", preprocessRequest(prettyPrint()), preprocessResponse(prettyPrint()),
pathParameters(
parameterWithName("username").description("The username's user")),
requestHeaders(
headerWithName("Cookie").description(
"The cookie that identifies the user"))
))
.andExpect(MockMvcResultMatchers.jsonPath("$.bouquets", hasSize(3)))
.andExpect(status().isOk());
//verify userService.findUserInfos is called
verify(userService).findUserInfos(anyString(), anyString());
// verify the parameters
assertEquals("userTest", userNameCaptor.getValue());
assertEquals("cookieForTest", cookieCaptor.getValue());
}
ドキュメントを生成するテストを実行すると、次のエラーが発生します。
org.springframework.restdocs.snippet.SnippetException: Headers with the following names were not found in the request: [Cookie]
at org.springframework.restdocs.headers.AbstractHeadersSnippet.validateHeaderDocumentation(AbstractHeadersSnippet.java:83)
at org.springframework.restdocs.headers.AbstractHeadersSnippet.createModel(AbstractHeadersSnippet.java:65)
at org.springframework.restdocs.snippet.TemplatedSnippet.document(TemplatedSnippet.java:64)
at org.springframework.restdocs.generate.RestDocumentationGenerator.handle(RestDocumentationGenerator.java:196)
at org.springframework.restdocs.mockmvc.RestDocumentationResultHandler.handle(RestDocumentationResultHandler.java:55)
at org.springframework.test.web.servlet.MockMvc$1.andDo(MockMvc.java:177)
at com.orange.otml.otmluser.controller.UsersControllerTest.getUserInfoOK(UsersControllerTest.java:183)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
at org.springframework.restdocs.JUnitRestDocumentation$1.evaluate(JUnitRestDocumentation.java:55)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:252)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:119)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
spring-restdoc はどのように Cookie を管理しますか? spring-restdoc を使用してアイデンティティ Cookie を文書化する方法は?
spring-restdoc でクッキー用に予約された単語が見つかりませんでした。
誰でも私を助けることができますか?