首页 > 解决方案 > 我们如何在 Java 中的 BDD 功能文件中将 JSON 对象列表作为 JSON 字符串传递

问题描述

下面是我的功能文件。

场景1:获取所有AppsCount

当我们请求所有应用程序时,我们得到 4 个应用程序,应用程序列表以 json 形式发送:

"[
  { '_id' : 'app1', 'Name' : 'App 1', 'Type' : 1, 'Rules' : [ { '_id' : 'Rule1', 'FilterCriteria' : 8 }, { '_id' : 'Rule2', 'FilterCriteria' : 4 } ], 'Email' : 'test@dell.com', 'IsSlackEnabled' : false, 'IsEmailEnabled' : true, 'IsSMSEnabled' : false, 'IsPhoneEnabled' : false, 'IsNonProdEnabled' : false, 'CreatedBy' : 'test@dell.com' },
  { '_id' : 'app2', 'Name' : 'App 2', 'Type' : 2, 'Rules' : [ { '_id' : 'Rule3', 'FilterCriteria' : 8 } ], 'Email' : 'test1@dell.com', 'IsSlackEnabled' : true, 'SlackChannel' : 'testChannel', 'IsEmailEnabled' : false, 'IsSMSEnabled' : false, 'IsPhoneEnabled' : true, 'IsNonProdEnabled' : false, 'CreatedBy' : 'test1@dell.com' },
  { '_id' : 'app3', 'Name' : 'App 3', 'Type' : 3, 'Rules' : [ { '_id' : 'Rule4', 'FilterCriteria' : 7 } ], 'Email' : 'test3@dell.com', 'IsSlackEnabled' : true, 'IsEmailEnabled' : false, 'SlackChannel' : 'testChannel2', 'IsSMSEnabled' : true, 'IsPhoneEnabled' : false, 'IsNonProdEnabled' : false, 'CreatedBy' : 'test3@dell.com' },
  { '_id' : 'app4', 'Name' : 'App 4', 'Type' : 1, 'Rules' : [], 'Email' : 'test4@dell.com', 'IsSlackEnabled' : false, 'IsEmailEnabled' : false, 'IsSMSEnabled' : false, 'IsPhoneEnabled' : true, 'IsNonProdEnabled' : false, 'CreatedBy' : 'test4@dell.com' }
]"

但这是一个问题,因为我们在编写 json 字符串时需要双引号

所以我尝试了以下功能文件。

场景 2:获取所有AppsCount

当我们请求所有应用程序时,我们得到 4 个应用程序,应用程序列表以 json 形式发送:

"[
  { "_id" : "app1", "Name" : "App 1", "Type" : 1, "Rules" : [{ "_id" : "Rule1", "FilterCriteria" : 8 }, { "_id" : "Rule2", "FilterCriteria" : 4 } ], "Email" : "test@dell.com", "IsSlackEnabled" : false, "IsEmailEnabled" : true, "IsSMSEnabled" : false, "IsPhoneEnabled" : false, "IsNonProdEnabled" : false, "CreatedBy" : "test@dell.com" },
  { "_id" : "app2", "Name" : "App 2", "Type" : 2, "Rules" : [ { "_id" : "Rule3", "FilterCriteria" : 8 } ], "Email" : "test1@dell.com", "IsSlackEnabled" : true, "SlackChannel" : "testChannel", "IsEmailEnabled" : false, "IsSMSEnabled" : false, "IsPhoneEnabled" : true, "IsNonProdEnabled" : false, "CreatedBy" : "test1@dell.com" },
  { "_id" : "app3", "Name" : "App 3", "Type" : 3, "Rules" : [ { "_id" : "Rule4", "FilterCriteria" : 7 } ], "Email" : "test3@dell.com", "IsSlackEnabled" : true, "IsEmailEnabled" : false, "SlackChannel" : "testChannel2", "IsSMSEnabled" : true, "IsPhoneEnabled" : false, "IsNonProdEnabled" : false, "CreatedBy" : "test3@dell.com" },
  { "_id" : "app4", "Name" : "App 4", "Type" : 1, "Rules" : [], "Email" : "test4@dell.com", "IsSlackEnabled" : false, "IsEmailEnabled" : false, "IsSMSEnabled" : false, "IsPhoneEnabled" : true, "IsNonProdEnabled" : false, "CreatedBy" : "test4@dell.com" }
] "

但是在创建步骤定义时,每个字段的值都会作为它自己的参数出现。

我知道

| field1 | field2 | 
| value1 | value2 |

解决方案,但我的数据太大了,我想知道是否有办法直接传递 JSON 字符串。

标签: javajsonspringbdd

解决方案


创建一个文件 text.json ..并将 json 输入放在文件中.. 将此文件放在类路径中..

在功能文件中将文件名放在这种情况下-text.json..

在类文件 stepdef 中读取文件名.. 在类路径中查找此文件..

使用 clasloader 加载此文件并使用 objectmapper 映射此 json。


推荐阅读