Java SpringREST文档-Junit测试中的fieldWithPath

Java SpringREST文档-Junit测试中的fieldWithPath,java,rest,spring-boot,spring-restdocs,Java,Rest,Spring Boot,Spring Restdocs,我有一个SpringBootV1.5.14.RELEASE应用程序,使用Spring初始值设定项、JPA、嵌入式Tomcat并遵循RESTfulAPI架构原则。我创建了这个测试 @Test public void createCustomerChain() throws Exception { this.mockMvc.perform(post("/customer/createCustomer") .contentType(MediaT

我有一个SpringBootV1.5.14.RELEASE应用程序,使用Spring初始值设定项、JPA、嵌入式Tomcat并遵循RESTfulAPI架构原则。我创建了这个测试

@Test
    public void createCustomerChain() throws Exception {

        this.mockMvc.perform(post("/customer/createCustomer")
                .contentType(MediaType.APPLICATION_JSON_VALUE)
                .content("{\n" +
                        "   \"subSegment\":\"25\",\n" +
                        "   \"legalLanguage\":\"NL\",\n" +
                        "   \"isRestrictel\":true,\n" +
                        "   \"isCommunicationLanguageForAllAccount\":true,\n" +
                        "   \"isAntiMarketing\":true,\n" +
                        "   \"hotelChain\":{\n" +
                        "       \"legalForm\":\"09\",\n" +
                        "       \"foundationDate\":\"2001-12-17T09:30:47Z\",\n" +
                        "       \"tradingName\":\"COMPANY NAME\",\n" +
                        "       \"printName\":\"TEST PRINT\",\n" +
                        "       \"naceCode\":\"16230\",\n" +
                        "       \"vatNumber\":\"41223334343\",  \n" +
                        "       \"countryVatCode\":\"IN\",\n" +
                        "       \"isSubjectToVAT\":true,\n" +
                        "       \"sectorCode\":\"85\",\n" +
                        "       \"legalAddress\": {\n" +
                        "           \"mainkey\":2088512,\n" +
                        "           \"subkey\":3256\n" +
                        "       }\n" +
                        "   },\n" +
                        "   \"isVATNumberOnBill\":true,\n" +
                        "   \"communicationLanguage\":\"EN\"\n" +
                        "}"))
                .andExpect(status().isOk())
                .andDo(document("customer-create-request-Chain",
                        preprocessRequest(prettyPrint()),
                        preprocessResponse(prettyPrint()),
                        responseFields(
                                fieldWithPath("billingAccountId").description("The billing account id of the newly created customer"),
                                fieldWithPath("paymentAgreementId").description("The Payment Agreement Id of the newly created customer"),
                                fieldWithPath("customerId").description("The id of the new created customer"),
                                fieldWithPath("isNewlyCreated").description("Set to `true` when the customer is newly created")),
                        requestFields(
                                fieldWithPath("subSegment").description("Market subsegment"),
                                fieldWithPath("legalLanguage").description("Legal language"),
                                fieldWithPath("isAntiMarketing").description("If true, customer does not want to have any marketing contact"),
                                fieldWithPath("isRestrictel").description("Indicates that the customer is on the orange list. Do not disclose information to other companies for commercial purposes."),
                                fieldWithPath("hotelChain.legalAddress.mainkey").description("LAM mainkey of the address. If the mainkey and the subkey is given the rest of the address information is not needed."),
                                fieldWithPath("hotelChain.legalAddress.subkey").description("LAM subkey of the address"),
                                fieldWithPath("hotelChain.legalForm").description("Legal form"),
                                fieldWithPath("hotelChain.foundationDate").description("Date of the foundation of a company(mandatory field for 'Chain' customer type)"),
                                fieldWithPath("hotelChain.vatNumber").description("Enterprise or VAT number"),
                                fieldWithPath("hotelChain.countryVatCode").description("ISO2 country code of the VAT number"),
                                fieldWithPath("isVATNumberOnBill").description("Indicates if the VAT number will be shown on the bill"),
                                fieldWithPath("hotelChain.isSubjectToVAT").description("Indicates if the enterprise number is a real VAT or not"),
                                fieldWithPath("hotelChain.sectorCode").description("Subtitle Description. Additional information relating to the customer. Name-Value pairs"),
                                fieldWithPath("hotelChain.tradingName").description("Trading name"),
                                fieldWithPath("hotelChain.printName").description("Print name of the customer"),
                                fieldWithPath("hotelChain.naceCode").description("Nace code"),
                                fieldWithPath("communicationLanguage").description("Communication language"),
                                fieldWithPath("isCommunicationLanguageForAllAccount")
                                        .description("If true, commercial language of all BGC billing accounts receive the value defined at customer level and users not allowed to change commercial language of any billing account"))));
    }
这是运行测试的结果:

org.springframework.restdocs.snippet.SnippetException: Fields with the following paths were not found in the payload: [customerId]

字段中删除
customerId
,路径测试成功通过,但是,我想知道为什么像
BillingAccount这样的字段没有相同的错误,这是因为
customerId
字段在某些情况下可能为
null
,或者不存在。您可以对其使用
可选

fieldWithPath("customerId").description("Description").optional()

要想找到更准确的原因,请发布
createCustomer()
方法的代码。

请给出响应?我只能猜测,正如控制台告诉我的那样,
customerId
字段在其他字段存在时丢失了。