【问题标题】:Groovy : Compare SOAP Response with XML fileGroovy:将 SOAP 响应与 XML 文件进行比较
【发布时间】:2016-11-22 13:53:00
【问题描述】:

我想在 groovy 代码中比较我的 Soap 响应与忽略顺序的 xml 文件:

这是我的代码:

import org.custommonkey.xmlunit.Stuff
import org.xmlunit.Stuff

//ExpectedString is my xml converted to text, same for ResponseString

Diff diff = DiffBuilder.compare(ExpectedString)
           .withTest(ResponseString)
           .ignoreComments()
           .ignoreWhitespace()
           .checkForSimilar()
           .withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.byName))
           .build();

assertFalse("XML similar " + diff.toString(), diff.hasDifferences())

所以,如您所见,我使用了 DefaultNodeMatcher,我使用了 XMLUnit2.0 ...没有结果(甚至没有忽略顺序或比较时出现异常错误)

有解决办法吗?解决这个问题

由于我迫切地想找到一个直接的,我可以对我的 xml 和我的肥皂响应进行排序,以便我可以有一个简单的差异吗?有没有办法按字母顺序逐行排序?如果是,如何?

谢谢你们!

更新:

这是我的 XML 结构简化

<body>
<stuff>
  <miniStuff></miniStuff>
  <miniStuff></miniStuff>
</stuff>
<Services>
  <Service>
    <tag1>ValueA</tag1>
    <tag2>ValueAA</tag2>
  </Service>
  <Service>
    <tag1>ValueB</tag1>
    <tag2>ValueBB</tag2>
  </Service>
</services>
</body>

我的问题是我不能保证 ValueA 是第一个而不是第二个

【问题讨论】:

  • 您有想要比较的样本数据吗?
  • 我会在一分钟内将一个简单的数据添加到我的帖子中,谢谢 :)
  • @Rao : 准备好了兄弟 :)

标签: xml groovy compare xmlunit xmlunit-2


【解决方案1】:

这是您可能正在寻找的:使用ByNameAndTextRecSelector

withNodeMatcher(new DefaultNodeMatcher(new ByNameAndTextRecSelector(),ElementSelectors.byName))

单元测试:

    @Test
    public void testDiffOrder() {
        final String control = """
            <r>
                <ser>
                    <t1>a</t1>
                    <t2>b</t2>
                </ser>
                <ser>
                    <t1>d</t1>
                    <t2>e</t2>
                </ser>
            </r>"""
        final String test = """
            <r>
                <ser>
                    <t1>d</t1>
                    <t2>e</t2>
                </ser>
                <ser>
                    <t1>a</t1>
                    <t2>b</t2>
                </ser>
            </r>"""
        Diff diff = DiffBuilder.compare(Input.fromString(control))
                .withTest(Input.fromString(test))
                .ignoreComments()
                .ignoreWhitespace()
                .checkForSimilar()
                .withNodeMatcher(new DefaultNodeMatcher(new ByNameAndTextRecSelector(),ElementSelectors.byName))
                .build()

        assertFalse("XML differ " + diff.toString(), diff.hasDifferences())
    }

感谢@bodewig。 来自here的样本

更新:更规范的版本带有 OP 原始 xml sn-p。

import org.xmlunit.builder.DiffBuilder
import org.xmlunit.builder.Input
import org.xmlunit.diff.ByNameAndTextRecSelector
import org.xmlunit.diff.DefaultNodeMatcher
import org.xmlunit.diff.ElementSelectors

def control = """<body>
   <stuff>
      <miniStuff />
      <miniStuff />
   </stuff>
   <Services>
      <Service>
         <tag1>ValueB</tag1>
         <tag2>ValueBB</tag2>
      </Service>
      <Service>
         <tag1>ValueA</tag1>
         <tag2>ValueAA</tag2>
      </Service>
   </Services>
</body>"""
def test = """<body>
   <stuff>
      <miniStuff />
      <miniStuff />
   </stuff>
   <Services>
      <Service>
         <tag1>ValueA</tag1>
         <tag2>ValueAA</tag2>
      </Service>
      <Service>
         <tag1>ValueB</tag1>
         <tag2>ValueBB</tag2>
      </Service>
   </Services>
</body>"""
def myDiff = DiffBuilder.compare(Input.fromString(control))
            .withTest(Input.fromString(test))
            .checkForSimilar()
            .withNodeMatcher(new DefaultNodeMatcher(new ByNameAndTextRecSelector(),ElementSelectors.byName))
            .build()
println myDiff.toString()
println myDiff.hasDifferences()
assert !myDiff.hasDifferences()

【讨论】:

  • @HamzaAmami,很高兴知道它很有帮助。
猜你喜欢
  • 2018-06-18
  • 1970-01-01
  • 2014-11-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多