【发布时间】:2020-02-06 18:59:10
【问题描述】:
我使用 Citrus 框架为通过 HTTP REST 通信的 Spring Boot 服务编写了一个集成测试。
我能够在 JSON 中嵌入一些 Citrus 验证“方法”,以处理像 @isNumber()@ 这样的情况,时间戳总是在变化。然而,当我尝试其他一些方法时,我遇到了问题。
JSON 中的另一个字段 (commandID) 包含 UUID,并且可能会从一个调用更改为另一个。我首先决定使用@match("<regex>")@ 来匹配一个UUID 模式,当它产生问题时,切换到尝试使用@ignore@,但这也产生了同样的问题。
这是来自控制台的错误:
13:29:21.962 [main] ERROR com.consol.citrus.report.LoggingReporter - TEST FAILED MissionPlannerIT.testPlanMission <edu.mit.ll.mission_services.service.mission_planner> Nested exception is:
com.consol.citrus.exceptions.TestCaseFailedException: Validation failed: Values not equal for header element 'Accept', expected 'application/json' but was 'application/json,application/*+json'
我不知道问题出在哪里,也不知道如何解决。
gen-route-command.json:
{
"header": {
"timestamp": "@isNumber()@",
"typeID": "edu.mit.ll.mission_services.messages.GenerateRouteCommand",
"transaction": {
"id": 1,
"startTime": "@isNumber()@"
},
"signature": {
"algorithm": null,
"keySize": 0,
"keyValue": null,
"sender": null
}
},
"commandID": "@ignore@",
"commandType": "GENERATE_ROUTE"
}
@isNumber()@ 函数工作并允许这些元素的匹配通过验证。 @ignore@,但没有。我尝试了两种变体:@ignore@ 和@ignore()@,都不起作用。如前所述,@match(...)@ 也不起作用。
更新:
我修改了一些东西,以便尝试在测试用例代码中进行验证。我将 JSON 中的值改回 UUID。
runner.http(builder -> builder
.server(routeGeneratorServer)
.receive()
.post("/v1/missionServices/missionPlanning/generateRoute")
.accept(ContentType.APPLICATION_JSON.getMimeType())
.payload(new ClassPathResource("templates/gen-route-command.json"))
.validate("$.commandID", "@ignore@"));
gen-route-command.json:
{
"header": {
"timestamp": "@isNumber()@",
"typeID": "edu.mit.ll.mission_services.messages.GenerateRouteCommand",
"transaction": {
"id": 1,
"startTime": "@isNumber()@"
},
"signature": {
"algorithm": null,
"keySize": 0,
"keyValue": null,
"sender": null
}
},
"commandID": "0710d523-43da-4f68-90c7-a2b4544a955d",
"commandType": "GENERATE_ROUTE"
}
很遗憾,这不起作用。我收到了我最初试图通过 @ignore@ 或 @match(...)@ 缓解的错误。
14:12:57.299 [main] ERROR com.consol.citrus.report.LoggingReporter - TEST FAILED MissionPlannerIT.testPlanMission <edu.mit.ll.mission_services.service.mission_planner> Nested exception is:
com.consol.citrus.exceptions.TestCaseFailedException: Failed to validate JSON text:
{"header":{"timestamp":1581016372132,"typeID":"edu.mit.ll.mission_services.messages.GenerateRouteCommand","transaction":{"id":1,"startTime":1581016372130},"signature":{"algorithm":null,"keySize":0,"keyValue":null,"sender":null}},"commandID":"9302ebde-894b-43a3-b7a3-92a158c7170e","commandType":"GENERATE_ROUTE"} Values not equal for entry: 'commandID', expected '0710d523-43da-4f68-90c7-a2b4544a955d' but was '9302ebde-894b-43a3-b7a3-92a158c7170e'
commandID 值不同,因为它每次运行都会生成一个新的 UUID。
似乎测试中的验证并未“忽略”该值,而是 JSON 验证标记了 UUID 差异。
【问题讨论】:
标签: java json http citrus-framework