简体   繁体   English

如何使用groovy在json文件中断言值?

[英]How to assert value in json file using groovy?

i want to assert values between 2 json files.我想在 2 个 json 文件之间断言值。 Here is the code i tried, it works fine but i have more than 300 values to test.这是我试过的代码,它工作正常,但我有 300 多个值要测试。 Instead of having 300 lines is there a better way to do it with a loop :不是有 300 行,而是有一个更好的方法来做一个循环:

file1.json content is: file1.json 内容为:

[
 {
   "Name": "Pierre",
   "Address": 1,
   "City": "Paris",
   "Country": "FRA",
   "Code": "2020-01-01T00:00:00",
   "Position": " 7000,00  $ "
 },
 {
   "Name": "Pierre",
   "Address": 2,
   "City": "Paris",
   "Country": "USA",
   "Code": "2020-01-01T00:00:00",
   "Position": " 9000,00  $ "
 },
 {
   "Name": "Pierre",
   "Address": 3,
   "City": "Paris",
   "Country": "GER",
   "Code": "2020-01-01T00:00:00",
   "Position": " 2000,00  $ "
 }
 ]

file2.json content is: file2.json 内容为:

{"value": {
    "data": {"number1": [ 
[
 {
   "Name": "Pierre",
   "Address": 1,
   "City": "Paris",
   "Country": "FRA",
   "Code": "2020-01-01T00:00:00",
   "Position": " 7000,00  $ "
 },
 {
   "Name": "Paul",
   "Address": 2,
   "City": "Paris",
   "Country": "USA",
   "Code": "2020-01-01T00:00:00",
   "Position": " 9000,00  $ "
 },
 {
   "Name": "Pierre",
   "Address": 3,
   "City": "Paris",
   "Country": "GER",
   "Code": "2020-01-01T00:00:00",
   "Position": " 2000,00  $ "
 },
   "Name": "Luc",
   "Address": 6,
   "City": "Pekin",
   "Country": "CHN",
   "Code": "2020-01-01T00:00:00",
   "Position": " 800,00  $ "
 },
 ]
 ]
 }

i want to assert each value of file1 with file2.我想用 file2 断言 file1 的每个值。 For exemple:举个例子:

JsonSlurper jsonSlurper1 = new JsonSlurper()
File file1Actual = new File('c:/temp/file1.json')
def actualJson = jsonSlurper1.parse(file1Actual)

JsonSlurper jsonSlurper2 = new JsonSlurper()
File file2Expected = new File('c:/temp/file2.json')
def expectedJson = jsonSlurper2.parse(file2Expected)

assert actualJson.value.data.number1.Name[0] == expectedJson.Name[0]
assert actualJson.value.data.number1.Name[1] == expectedJson.Name[1]
assert actualJson.value.data.number1.Name[2] == expectedJson.Name[2]

Thank you for any suggestions谢谢你的任何建议

Assuming you are actually coding in Groovy (not Java) as your code example suggests, something like the following should do it:假设您实际上是在 Groovy(而不是 Java)中编码,正如您的代码示例所建议的那样,应该执行以下操作:

/* ... read in the Json files as outlined in the question ... */
actualJson.value.data.number1.size.times {
    // Just to demonstrate that 'it' takes on values 0, 1, 2, 3, 4 etc.
    println "assert number ${it}"
    assert actualJson.value.data.number1.Name[it] == expectedJson.Name[it]
}

However this is a very 'groovyesque' way of doing it.然而,这是一种非常“常规”的方式。 If you want something a tad closer what a Java developer would write, you would maybe do:如果你想要更接近 Java 开发人员会写的东西,你可能会这样做:

/* read in Jsons, then: */
// Let's assign the list to a var for better readability
def actualList = actualJson.value.data.number1

// Bonus points for asserting that both lists are of equal size
// assert actualList.size == expectedJson.size
// If this does not hold then
def smaller = Math.min(actualList.size, expectedJson.size)

// a classic for-loop.
for (int i = 0; i < smaller; i++) {
    println "assert number ${i}"
    assert actualList[i].Name == expectedJson[i].Name
}

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

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