user2201789
user2201789

Reputation: 1203

missing property in assertion, which assertion should be used to check json one by one

my application database colA stored data as below:

{
  "A": "A ABC123",
  "B": "B ABC123",
  "C": "C ABC123",
  "D": "D ABC123"
}

I have written unit test codes to check the response:

//SQL statement
String dbQuery = "SELECT * FROM items.A where id = 'ABC123' order by lastmodificationdateutc desc"


//Connect to SQL Server, global variable is stored at profile
List results = CustomKeywords.'swisslog.database.getSQLResults'(GlobalVariable.dbConnString , GlobalVariable.dbUsername , GlobalVariable.dbPassword ,GlobalVariable.dbDriver ,dbQuery )

//print the "operationnotice" column
String colA = results.get(0).get('colA')
println(colA)

assertThat(colA.A).isEqualTo("A ABC123")
assertThat(colA.B).isEqualTo("B ABC123")
assertThat(colA.C).isEqualTo("C ABC123")
assertThat(colA.D).isEqualTo("D ABC123")

error returned as below:

groovy.lang.MissingPropertyException: No such property: A for class: java.lang.String

i can see printin correctly

2019-10-29 16:14:26.979 DEBUG testcase.1
- 5: println(colA)
{
  "A": "A ABC123",
  "B": "B ABC123",
  "C": "C ABC123",
  "D": "D ABC123"
}

Upvotes: 0

Views: 129

Answers (1)

user2201789
user2201789

Reputation: 1203

found answer, i added some codes to parse json then assert them

def json = new JsonSlurper().parseText(new String(colA))
println('response text: \n' + JsonOutput.prettyPrint(JsonOutput.toJson(colA)))


assertThat(json.A).isEqualTo("A ABC123")
assertThat(json.B).isEqualTo("B ABC123")
assertThat(json.C).isEqualTo("C ABC123")
assertThat(json.D).isEqualTo("D ABC123")

Upvotes: 1

Related Questions