最近、spring-mvc-test-framework を使用して作業を行うと、次のようなコードが表示されます。
this.mockMvc.perform(MockMvcRequestBuilders.get("/manage/bill/detail/{id}","5507c2240cf2bc43ba2367bd").accept(MediaType.ALL)).andExpect(MockMvcResultMatchers.status().isOk()).andDo(MockMvcRestDocumentation.document("index"));
その後、このエラーが発生しました。
java.lang.IllegalArgumentException: No InputStream specified
at org.springframework.util.Assert.notNull(Assert.java:112)
at org.springframework.util.FileCopyUtils.copy(FileCopyUtils.java:106)
at org.springframework.util.FileCopyUtils.copyToByteArray(FileCopyUtils.java:156)
at org.springframework.restdocs.mockmvc.MockMvcOperationRequestFactory.createOperationRequest(MockMvcOperationRequestFactory.java:83)
at org.springframework.restdocs.mockmvc.RestDocumentationResultHandler.handle(RestDocumentationResultHandler.java:93)
at org.springframework.test.web.servlet.MockMvc$1.andDo(MockMvc.java:155)
検索しても答えが見つかりません。なぜこれが起こるのか誰か教えてもらえますか?
今、私は別の解決策を得ました。
実際、公式リファレンスhttp://docs.spring.io/spring-restdocs/docs/1.0.0.RELEASE/reference/html5/#getting-started-build-configuration-maven-packagingでSpring MVC テスト例を使用しています。次のように setUp() メソッドを使用します。
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context).build();
目的は api を文書化することです。もう 1 つの解決策は、apply() メソッドを使用して問題を解決することです。
まず、次のように変更しました:
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
.apply(MockMvcRestDocumentation.documentationConfiguration(this.restDocumentation)).build();
次に、次のようなバージョンの問題を解決しました: spring-restdocs は apply() を認識していません
以下のすべての私のコード:
package com.dream.bwm.test.test;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.restdocs.RestDocumentation;
import org.springframework.restdocs.mockmvc.MockMvcRestDocumentation;
import org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(locations = { "classpath:spring/root-context.xml", "classpath:spring/app-context.xml" })
public class DocsGenerator {
public static String outputDir = "D:\\workspace-bwme\\pro-root\\bwme-backend\\target\\generated-snippets";
@Rule
public final RestDocumentation restDocumentation = new RestDocumentation(outputDir);
@Autowired
private WebApplicationContext context;
private MockMvc mockMvc;
@Before
public void setUp() {
// this.mockMvc =
// MockMvcBuilders.webAppContextSetup(this.context).build();
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
.apply(MockMvcRestDocumentation.documentationConfiguration(this.restDocumentation)).build();
}
// @Test
public void printOnConsole() throws Exception {
this.mockMvc
.perform(
MockMvcRequestBuilders.get("/manage/bill/detail/{id}", "5507c2240cf2bc43ba2367bd").accept(
MediaType.ALL_VALUE)).andDo(MockMvcResultHandlers.print())
.andExpect(MockMvcResultMatchers.status().isOk());// MockMvcRestDocumentation.document("index")
}
@Test
public void writeToDocument() throws Exception {
this.mockMvc
.perform(
RestDocumentationRequestBuilders.get("/manage/bill/detail/{id}", "5507c2240cf2bc43ba2367bd")
.accept(MediaType.ALL)).andExpect(MockMvcResultMatchers.status().isOk())
.andDo(MockMvcRestDocumentation.document("index"));
}
}
誰かがそのような質問に遭遇した場合、これがあなたの助けになることを願っています.