2017-10-09 3 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의 끝으로 비교하는 경우와 같이를 false으로 표시하고 json.policies의 마지막 isActive는 true입니다.

+0

Json지도에 주문 번호 –

+0

@ ok ok가 없습니다. 그런 다음 어떻게 가끔 전달되고 어설 션에 실패합니까? 내가 코드에서 잘못한 부분이 있습니까? –

+0

가끔은 올바른 순서로 나오기 때문에, 다른 것들은 나오지 않기 때문입니다. –

답변

1

거의 비슷합니다. 그러나, 정정해야 할 몇 가지.

  • 당신은 jsontoString을 변환 할 필요가 없습니다.
  • 비교하기 전에 목록을 정렬해야합니다.
  • 각지도 항목이 로그에 표시된 순서와 상관없이 각지도를 비교할 수 있습니다. 여기

당신은 신속하게 온라인으로 귀하의 설명에 따라 고정 데이터 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' 

입니다.

관련 문제