Я бы хотел автоматически генерировать заглушки WireMock с помощью REST Docs.Я хочу более точно соответствовать запросу multipart/form-data
, используя WireMockRestDocs.verify()
, но всегда получаю исключение [wiremock did not match request]
.В моем API есть метод, который объединяет две части: объект json и MultipartFile.Что не так?
spring-boot 2.0.6.RELEASE
spring-cloud-dependencies Finchley.SR2
Контроллер
@RestController
public class MultipartTestController {
private final Log logger = LogFactory.getLog(getClass());
@PostMapping("/test")
public ResponseEntity<Void> postMultipart(@RequestPart("metadata") MetadataDTO metadataDTO, @RequestPart("file") MultipartFile multipartFile) {
logger.info(metadataDTO);
return ResponseEntity.noContent().build();
}
}
Тест
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureRestDocs(outputDir = "target/generated-snippets")
@AutoConfigureMockMvc
public class DemoRestdocMultipartApplicationTests {
@Autowired
private MockMvc mockMvc;
@Autowired
private ObjectMapper objectMapper;
@Test
public void contextLoads() throws Exception {
MetadataDTO metadataDTO = new MetadataDTO();
metadataDTO.setName("test");
MockMultipartFile file = new MockMultipartFile("file", "contract.pdf", MediaType.APPLICATION_PDF_VALUE, "pdfcontent".getBytes(StandardCharsets.UTF_8));
MockMultipartFile metadata = new MockMultipartFile("metadata", "", MediaType.APPLICATION_JSON_VALUE, objectMapper.writeValueAsString(metadataDTO).getBytes(StandardCharsets.UTF_8));
this.mockMvc.perform(fileUpload("/test")
.file(file)
.file(metadata))
.andExpect(status().isNoContent())
.andDo(verify()
.wiremock(
post(WireMock.urlPathEqualTo("/test"))
.withHeader(HttpHeaders.CONTENT_TYPE, WireMock.containing(MediaType.MULTIPART_FORM_DATA_VALUE))
.withMultipartRequestBody(
aMultipart()
.withHeader("Content-Disposition", containing("name=file"))
.withHeader("Content-Type", WireMock.equalTo(MediaType.APPLICATION_PDF_VALUE))
.withBody(equalTo("pdfcontent"))
)
.withMultipartRequestBody(
aMultipart()
.withHeader("Content-Disposition", containing("name=metadata"))
.withHeader("Content-Type", WireMock.containing(MediaType.APPLICATION_JSON_VALUE))
.withBody(WireMock.matchingJsonPath("$[?(@.name == 'test')]"))
)
).stub("testMultipart"))
.andDo(document("testMultipart",
preprocessRequest(prettyPrint()),
requestParts(
partWithName("file").description("The file to upload"),
partWithName("metadata").description("metadata")
),
requestPartFields("metadata",
fieldWithPath("name").description("name")
)
, dslContract()
));
}
}
Исключение
org.junit.ComparisonFailure: [wiremock did not match request]
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at org.springframework.cloud.contract.wiremock.restdocs.WireMockVerifyHelper.configure(WireMockVerifyHelper.java:76)
at org.springframework.cloud.contract.wiremock.restdocs.ContractResultHandler.handle(ContractResultHandler.java:42)
at org.springframework.test.web.servlet.MockMvc$1.andDo(MockMvc.java:183)
at sk.vub.demo.multipart.demorestdocmultipart.DemoRestdocMultipartApplicationTests.contextLoads(DemoRestdocMultipartApplicationTests.java:55)
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:498)
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.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:73)
at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:83)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
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:251)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
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:190)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)