通过使用与多部分/表单数据不兼容的REST文档来生成存根

时间:2018-11-29 09:10:28

标签: spring-cloud-contract spring-restdocs

我想通过使用REST文档自动生成WireMock存根。我想通过使用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)

0 个答案:

没有答案