2017-10-09 10 views
1

私は自分のSOAP UIスクリプトで珍しいものを見ました。私は正しいことをしてデータをので、私は以下のこのコード書かれているという主張を実行したい:groovy経由でjson出力を注文するには?

import com.eviware.soapui.support.GroovyUtils 
import groovy.json.JsonOutput 
import groovy.json.JsonSlurper 
def response = messageExchange.response.responseContent 
def json = new JsonSlurper().parseText(response) 
def jsonFormat = (response).toString() 

def policies = [ 
    [x: 28, xxx: 41, xxxxx: 1, name: 'Individual 18-50', aaa: true], 
    [x: 31, xxx: 41, xxxxx: 1, name: 'Individual 51-60', aaa: true], 
    [x: 34, xxx: 41, xxxxx: 1, name: 'Individual 61-75', aaa: true], 
    [x: 37, xxx: 41, xxxxx: 1, name: 'Individual 76-85', aaa: false] 
] 

log.warn json.policies 
log.error policies 
assert json.policies == policies 

私はlog.warnとlog.error情報を見て、それは正しくないでJSONレスポンスを表示します最初に 'isActive'フィールドが表示されます。

log.warn json.policiesディスプレイは、この:

[{aaa=true, xx=28, xxxxx=1, name=Individual 18-50, xxxx=41}, {aaa=true, x=31, xxxxx=1, name=Individual 51-60, xxx=41}, {aaa=true, x=34, xxxxx=1, name=Individual 61-75, xxx=41}, {aaa=true, x=37, xxxxx=1, name=Individual 76-85, xxx=41}] 

log.error policies表示する。この:私はそれようのDTOがjson.policiesで正しい順序内に表示させることができますどのように

[{x=28, xxx=41, xxxxx=1, name=Individual 18-50, aaa=true}, {x=31, xxx=41, xxxxx=1, name=Individual 51-60, aaa=true}, {x=34, xxxx=41, xxxxxx=1, name=Individual 61-75, aaa=true}, {x=37, xxx=41, xxxxx=1, name=Individual 76-85, aaa=false}] 

ポリシーとして正しい順序で表示されますか?

もう一つ珍しいことですが、テストケースを10回実行し、このアサーションチェックが10回のうち3回通過したテストステップを実行しました。最後のDTOをpoliciesの最後と比較した場合と同じように、isActivefalseと表示し、最後のisActiveはjson.policiesになります。trueです。

+0

Jsonの地図には注文 –

+0

がありません@オハイオ州オハイオ州オハイオ州それでは、時々合格し、アサーションに失敗するのはどうですか?私がコードで間違って行ったことはありますか? –

+0

時々それは正しい順序で出てくるので、他のものは出てこない。 –

答えて

1

あなたはほぼ間近です。しかし、修正することのカップル。

  • あなたはjsonのtoStringを変換する必要はありません。
  • リストを比較する前にソートする必要があります。
  • 各マップエントリがログに表示される順番は関係なく、各マップを比較できます。ここで

はあなたがすぐにオンラインであなたの説明に基づいて、固定データとdemoが、それは比較の作業を確認することができますScript Assertion

//Check if the response is empty or not 
assert context.response, 'Response is empty' 

def json = new groovy.json.JsonSlurper().parseText(context.response) 

//Assign the policies from current response; assuming above json contains that; change below statement otherwise. 
def actualPolicies = json.policies 
//Expected Polities 
def expectedPolicies = [ 
    [id: 28, providerId: 41, coverTypeId: 1, name: 'Individual 18-50', isActive: true], 
    [id: 31, providerId: 41, coverTypeId: 1, name: 'Individual 51-60', isActive: true], 
    [id: 34, providerId: 41, coverTypeId: 1, name: 'Individual 61-75', isActive: true], 
    [id: 37, providerId: 41, coverTypeId: 1, name: 'Individual 76-85', isActive: false] 
] 

log.info "List from response $actualPolicies" 
log.info "List from policies $expectedPolicies" 

//Sort the list and compare; each item is a map and using id to compare both 
assert expectedPolicies.sort{it.id} == actualPolicies.sort{it.id}, 'Both policies are not matching' 

です。

関連する問題