繁体   English   中英

Spring REST Docs-Junit测试中的fieldWithPath

[英]Spring REST Docs - fieldWithPath in Junit Test

我有一个Spring Boot v1.5.14.RELEASE应用程序,使用Spring Initializer,JPA,嵌入式Tomcat并遵循RESTful API架构原则。 我已经创建了这个测试

@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]

并从fieldWithPath测试中成功删除了customerId ,但我想知道为什么对于billingAccountId这样的字段没有相同的错误

这是由于在某些情况下customerId字段可能为null或不存在。 您可以使用optional的:

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM