gpt4 book ai didi

java - Spring REST 文档 - Junit 测试中的 fieldWithPath

转载 作者:行者123 更新时间:2023-11-30 05:32:45 25 4
gpt4 key购买 nike

我有一个 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]

并删除 customerId来自fieldWithPath测试成功通过,但是,我想知道为什么对于像 billingAccountId 这样的字段没有相同的错误

最佳答案

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

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

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

关于java - Spring REST 文档 - Junit 测试中的 fieldWithPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57216864/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com