当前位置: 首页 > 知识库问答 >
问题:

无法在rest-assured中验证Json架构

郭鸿信
2023-03-14

我正在使用rest-assured并使用它验证JSON模式。我使用jsonschema生成了模式。net(草稿7),并将生成的模式保存在文件中,并根据GETAPI的响应匹配生成的模式。但抛出错误的代码输出如下:

Actual: {"success":true,"message":"FacilityAttributes List","error":{},"result":[{"id":"aa2cdb2e-89cd-4970-a976-42df3349df74","name":"INTERNATIONAL","rank":null,"data_type":"BOOLEAN","default_value":"false","mandatory":false,"business_unit":"TRA","options":null,"facility_type":null,"group_name":"BusinessType","description":"Please check if facility is international!"},{"id":"144e7c1c-9df4-4c65-ae7e-685013566731","name":"HEAVY","rank":null,"data_type":"BOOLEAN","default_value":"false","mandatory":false,"business_unit":"TRA","options":null,"facility_type":null,"group_name":"BusinessType","description":"Please check if facility can handle heavy goods!"},{"id":"ca20c185-4c9a-425e-82e4-a5b244b5ef9d","name":"B2B","rank":5,"data_type":"BOOLEAN","default_value":"false","mandatory":false,"business_unit":"TRA","options":null,"facility_type":null,"group_name":"BusinessType","description":null},{"id":"12a2c7f3-7da7-4682-953c-2e06ab8c8335","name":"B2C","rank":4,"data_type":"BOOLEAN","default_value":"false","mandatory":false,"business_unit":"TRA","options":null,"facility_type":null,"group_name":"BusinessType","description":null},{"id":"34ef64ee-33dc-43ea-9ef0-722aeb2377f8","name":"HLD","rank":2,"data_type":"BOOLEAN","default_value":"false","mandatory":false,"business_unit":"TRA","options":null,"facility_type":null,"group_name":"BusinessType","description":null},{"id":"5147ec36-59ac-4843-9f46-a881548e33aa","name":"dock_count","rank":null,"data_type":"INTEGER","default_value":"0","mandatory":false,"business_unit":"TRA","options":null,"facility_type":null,"group_name":"Facility Attributes","description":"Dock Door count in Facility!"},{"id":"1bdd7e0f-3a47-4d57-b641-439ba8790e48","name":"capacity","rank":null,"data_type":"INTEGER","default_value":"0","mandatory":false,"business_unit":"TRA","options":null,"facility_type":null,"group_name":"Facility Attributes","description":"Facility Capacity!"},{"id":"16e81b0c-fe3b-4827-bcf0-dbfd6f2cc386","name":"dimention_unit","rank":null,"data_type":"OPTION","default_value":"CM","mandatory":false,"business_unit":"TRA","options":["CM","IN"],"facility_type":null,"group_name":"Facility Attributes","description":"Dimention unit on facility works!"},{"id":"75569bc1-f5f0-40c1-8e36-f3ef88940486","name":"weight_unit","rank":null,"data_type":"OPTION","default_value":"KG","mandatory":false,"business_unit":"TRA","options":["KG","GM","LB"],"facility_type":null,"group_name":"Facility Attributes","description":"Weight unit on facility works!"},{"id":"48b121ba-e142-4f10-955c-12be4c33f787","name":"sort_code","rank":null,"data_type":"TEXT","default_value":null,"mandatory":false,"business_unit":"TRA","options":null,"facility_type":null,"group_name":"Facility Attributes","description":"Facility sort code!"},{"id":"dc5cc52c-2f65-4b92-beaa-d8165dfdc157","name":"slab_rate","rank":null,"data_type":"FLOAT","default_value":"0","mandatory":false,"business_unit":"TRA","options":null,"facility_type":null,"group_name":"Facility Attributes","description":"Slab rate of facility"},{"id":"d6b3ac6d-5f97-46a5-8e71-72b96f2aa419","name":"slab_type","rank":null,"data_type":"OPTION","default_value":"Incentive Based","mandatory":false,"business_unit":"TRA","options":["Incentive Based","Fuel Based"],"facility_type":null,"group_name":"Facility Attributes","description":"Slab rate of facility"},{"id":"03654f31-150f-4420-837c-f22bda1bc476","name":"ProductivityBenchmark","rank":null,"data_type":"INTEGER","default_value":"0","mandatory":false,"business_unit":"TRA","options":null,"facility_type":null,"group_name":"Productivity","description":"Productivity Benchmark of facility!"},{"id":"ad471316-0858-4ed7-b817-7b554c780536","name":"ReverseDispatchCutoff","rank":null,"data_type":"TIME","default_value":"09:00","mandatory":false,"business_unit":"TRA","options":null,"facility_type":null,"group_name":"Facility Timings","description":"Reverse Dispatch Cutoff time"},{"id":"18041d87-a88d-4ed3-b457-9c6e96c2c250","name":"ForwardDispatchCutoff","rank":null,"data_type":"TIME","default_value":"12:00","mandatory":false,"business_unit":"TRA","options":null,"facility_type":null,"group_name":"Facility Timings","description":"Forward Dispatch Cutoff time"},{"id":"c3e677a7-5b21-4c6b-8384-27c936720177","name":"Implant_enabled","rank":13,"data_type":"BOOLEAN","default_value":"false","mandatory":false,"business_unit":"TRA","options":null,"facility_type":null,"group_name":"Flags","description":"Please check Implant is enabled in Facility!"},{"id":"0fcd0fbe-c448-455a-b825-8b1eb2fcc8b3","name":"DG_enabled","rank":12,"data_type":"BOOLEAN","default_value":"false","mandatory":false,"business_unit":"TRA","options":null,"facility_type":null,"group_name":"Flags","description":"Please check DG is enabled in Facility!"},{"id":"34067f20-ece0-4775-8596-4447f74c5084","name":"FMODx_enabled","rank":11,"data_type":"BOOLEAN","default_value":"false","mandatory":false,"business_unit":"TRA","options":null,"facility_type":null,"group_name":"Flags","description":"Please check Odx FM is enabled in Facility!"},{"id":"6237f744-e685-4aca-906a-48bd1bad9160","name":"FEApp_enabled","rank":9,"data_type":"BOOLEAN","default_value":"false","mandatory":false,"business_unit":"TRA","options":null,"facility_type":null,"group_name":"Flags","description":"Please check FE App is enabled in Facility!"},{"id":"5ceb5fd0-74db-46fc-97a1-afbbaabc3686","name":"PayTM_enabled","rank":8,"data_type":"BOOLEAN","default_value":"false","mandatory":false,"business_unit":"TRA","options":null,"facility_type":null,"group_name":"Flags","description":"Please check if PayTM is enabled in facility!"},{"id":"f69817cb-8662-4f38-b44b-fb875703e9cd","name":"CardOnDelivery_enabled","rank":7,"data_type":"BOOLEAN","default_value":"false","mandatory":false,"business_unit":"TRA","options":null,"facility_type":null,"group_name":"Flags","description":"Please check if Card On Delivery is enabled in facility!"},{"id":"a3bcf54b-d734-4f95-92fd-a15bb2a00be3","name":"BagGuide_enabled","rank":6,"data_type":"BOOLEAN","default_value":"false","mandatory":false,"business_unit":"TRA","options":null,"facility_type":null,"group_name":"Flags","description":"Please check if BagGuide is enabled in facility!"},{"id":"19cfc5d1-f5fb-4666-a13f-7519588e3bb2","name":"LMOdx_enabled","rank":5,"data_type":"BOOLEAN","default_value":"false","mandatory":false,"business_unit":"TRA","options":null,"facility_type":null,"group_name":"Flags","description":"Please check if LMOdx is enabled in facility!"},{"id":"dc99d927-5925-4b09-a686-9c0231259337","name":"Constellation_enabled","rank":4,"data_type":"BOOLEAN","default_value":"false","mandatory":false,"business_unit":"TRA","options":null,"facility_type":null,"group_name":"Flags","description":"Please check if Constellation is enabled in facility!"},{"id":"41ba47fe-ec23-4649-bf0b-51101e631e68","name":"Bagging_enabled","rank":3,"data_type":"BOOLEAN","default_value":"false","mandatory":false,"business_unit":"TRA","options":null,"facility_type":null,"group_name":"Flags","description":"Please check if Bagging is enabled in facility!"},{"id":"7ead8afd-2e98-4330-8e15-40d4cb944a3c","name":"MidMile_enabled","rank":2,"data_type":"BOOLEAN","default_value":"false","mandatory":false,"business_unit":"TRA","options":null,"facility_type":null,"group_name":"Flags","description":"Please check if MidMile is enabled in facility!"},{"id":"4b9ffcab-b5df-4f19-bbd9-52e208168039","name":"Sorter_enabled","rank":1,"data_type":"BOOLEAN","default_value":"false","mandatory":false,"business_unit":"TRA","options":null,"facility_type":null,"group_name":"Flags","description":"Please check if sorter is enabled in facility!"},{"id":"c81e981d-2c44-4bd7-9cc9-5ea20a3eaa3a","name":"Can_Handle_Palletised_Load","rank":null,"data_type":"BOOLEAN","default_value":"false","mandatory":false,"business_unit":"TRA","options":null,"facility_type":null,"group_name":"Flags","description":"Check if facility Can Handle Palletised Load"}]}

at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.codehaus.groovy.reflection.CachedConstructor.invoke(CachedConstructor.java:83)
at org.codehaus.groovy.reflection.CachedConstructor.doConstructorInvoke(CachedConstructor.java:77)
at org.codehaus.groovy.runtime.callsite.ConstructorSite$ConstructorSiteNoUnwrap.callConstructor(ConstructorSite.java:84)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallConstructor(CallSiteArray.java:60)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:235)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:247)
at io.restassured.internal.ResponseSpecificationImpl$HamcrestAssertionClosure.validate(ResponseSpecificationImpl.groovy:471)
at io.restassured.internal.ResponseSpecificationImpl$HamcrestAssertionClosure$validate$1.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:125)
at io.restassured.internal.ResponseSpecificationImpl.validateResponseIfRequired(ResponseSpecificationImpl.groovy:643)
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.codehaus.groovy.runtime.callsite.PogoMetaMethodSite$PogoCachedMethodSiteNoUnwrapNoCoerce.invoke(PogoMetaMethodSite.java:210)
at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite.callCurrent(PogoMetaMethodSite.java:59)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:52)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:154)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:166)
at io.restassured.internal.ResponseSpecificationImpl.content(ResponseSpecificationImpl.groovy:94)
at io.restassured.specification.ResponseSpecification$content$0.callCurrent(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:52)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:154)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:174)
at io.restassured.internal.ResponseSpecificationImpl.body(ResponseSpecificationImpl.groovy:244)
at io.restassured.internal.ValidatableResponseOptionsImpl.body(ValidatableResponseOptionsImpl.java:262)
at com.ums.api.AppTest.GetserData(AppTest.java:46)
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.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:108)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:669)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:877)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1201)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
at org.testng.TestRunner.privateRun(TestRunner.java:776)
at org.testng.TestRunner.run(TestRunner.java:634)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:425)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:420)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:385)
at org.testng.SuiteRunner.run(SuiteRunner.java:334)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1318)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1243)
at org.testng.TestNG.runSuites(TestNG.java:1161)
at org.testng.TestNG.run(TestNG.java:1129)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)

我的通用模式是模式链接

api API repsonse的响应

共有1个答案

隆宏爽
2023-03-14

也许目前的保证库工作到草案4。我正在使用json架构生成器工具在草案4中生成json架构,并能够验证响应。

安装和生成架构的命令:-

npm install -g json-schema-generator
json-schema-generator path/to/input_response.json -o path/to/json_schema.json

参考链接: https://github.com/krg7880/json-schema-generator

 类似资料:
  • 我有一个关于Rest保证的问题。-https://code.google.com/p/rest-assured/wiki/Usage 我知道我可以使用REST保证进行HTTP调用(. get. put等)。)并使用时()等验证响应。我想验证我已经保存在数据库中的JSON响应,而不是实时调用Web服务并验证它的响应。 我可以使用REST-保证加载JSON响应作为字符串并验证它吗?

  • 我知道rest assure中有很多关于验证的信息,但不幸的是,我无法找到我的问题的确切解决方案。 我正在训练以验证以下JSON的响应: 具有以下代码: 期望值返回 g1 和 g2 的列表。 我得到的错误是: 我怎样才能做到这一点?

  • 我们在应用程序中使用CAS(Central Authentication Service)进行身份验证。 我们有很多REST API,我们需要测试REST调用。我们正在计划建立一个测试套件来测试所有其余的API。所以,我们想到为您的套房使用“放心”。 问题是身份验证。由于我们已经有了CAS,对于每个调用,它会重定向到请求用户名和密码的登录页面。是否有一个直接通过用户名和密码在一个镜头,并得到身份验

  • 环境:Java,泽西,jackson,汤卡特。 我有一个rest服务从客户端获取JSON输入。我想验证输入,如果它是JSON,然后它是JSON,然后JSON输入中的键是预期的或不是。如果是,那么它将产生HTTP400-bad请求。 比如-- 例如-非json输入如 null 我的代码适用于前面提到的情况2,但我希望它也适用于情况1。当非JSON输入存在时,我希望它将凭据对象设置为null,然后我可

  • 我有一个简单的springboot应用程序和一个Rest api。我想验证请求参数不为null/空。我将json转换成一个java对象,并从这里开始验证它们是否包含所有必需的字段,并且不为null或不为空。(对象没有保存到db)我目前正在使用javax验证方法,但是没有成功。 我还有一个包含的要素类,我需要验证(所有字段均为必填字段,而不是空或空)

  • REST-assured 用于方便 REST 服务测试的 JAVA DSL。 举例如下: Here's an example of how to make a GET request and validate the JSON or XML response: get("/lotto").then().assertThat().body("lotto.lottoId", equalTo(5));