hamcrest - 如何否定 hasXPath 来测试文档不包含某些子节点?

问题描述 投票:0回答:2

我想测试某些 XML 文档包含某些 XPath,我应该如何在使用 Hamcrest 匹配器的单元测试中断言?

例如,我想断言文档在

foo/bar
下没有
pos
:

    assertThat(document, Matchers.hasXPath(
            "/svc_result/slia/pos/foo/bar)]")  # negate here
    );
unit-testing hamcrest
2个回答
1
投票
import org.hamcrest.MatcherAssert;
import org.hamcrest.core.IsNot;
import org.xmlunit.matchers.HasXPathMatcher;
// ...
        MatcherAssert.assertThat(document, IsNot.not(HasXPathMatcher.hasXPath("/svc_result/slia/pos/foo/bar)]")));

0
投票

我们应该使用 XPath negate,因为 Hamcrest 不支持匹配器否定:

    assertThat(document, Matchers.hasXPath(
            "/svc_result/slia/pos[not(foo/bar)]")
    );

parent[not(xxx)]
表示“此 XPath 父节点不包含子节点 xxx”。

© www.soinside.com 2019 - 2024. All rights reserved.