1

次のような XML を返す REST Web サービスがある Spock テストを作成しています。

<templates>
 <userTemplate id="1109">
  <settingsXml/>
  <type>USER</type>
  <label>template111</label>
  <description>template111</description>
 </userTemplate>
 <userTemplate id="1141" isAutomaticTemplate="true">
     <settingsXml/>
  <type>USER</type>
  <label>An updated user template</label>
 </userTemplate>
</templates>

私のテストでは、特定の userTemplate がこのドキュメントにないことを確認したいと考えています。そこで、HTTP Builder の REST クライアントと XMLSlurper を使用して、次のことを行っています。

   res = settingsService.get(path: "templates")
   res.status == 200
   def delTemplate = res.data.userTemplate.find {
    println it.@id == newUserTemplateId
    it.@id == newUserTemplateId
   }
   delTemplate

findを呼び出した後、 delTemplateは null になると考えていました (その ID を持つテンプレートがないため、式 println it.@id == newUserTemplateId は常にfalseを出力します。この場合、newUserTemplateIdの値は 1171 です)。 ただし、delTemplateは groovy.util.slurpersupport.NoChildren 型であり、userTemplate 要素が含まれているようです。

おもしろいことに、(REST から読み取るのではなく) テキストと同じ XML を使用して簡単なスクリプトを作成すると、res.userTemplate.find { it.@id == 1171 }予想どおり null が返されます。

私は何を間違っていますか、またはどうすればこれを解決できますか?

4

1 に答える 1

2

残りの Web サービスの JUnit テストには、httpBuilder と XMLSlurper を使用します。GPathResult の find() は常に別の GPathResult を返すという落とし穴がありますが、これには子が含まれていない可能性があります。

あなたの特定のユースケースでは、私が使用するイディオムは次のようになります。

def resp = settingsService.get(path: 'templates')
assert resp.success
assert resp.data.userTemplate.find {it.@id == newUserTemplateId}.isEmpty()
于 2011-08-01T13:37:22.437 に答える