Class HasXPath

All Implemented Interfaces:
Matcher<Node>, SelfDescribing

public class HasXPath extends TypeSafeDiagnosingMatcher<Node>
Applies a Matcher to a given XML Node in an existing XML Node tree, specified by an XPath expression.
  • Field Details

    • NO_NAMESPACE_CONTEXT

      public static final NamespaceContext NO_NAMESPACE_CONTEXT
      Null NamespaceContext, used to document deliberate use of no namespace.
  • Constructor Details

    • HasXPath

      public HasXPath(String xPathExpression, Matcher<String> valueMatcher)
      Constructor, best called from one of the hasXPath static factory methods.
      Parameters:
      xPathExpression - the target xpath
      valueMatcher - matcher for the expected value
    • HasXPath

      public HasXPath(String xPathExpression, NamespaceContext namespaceContext, Matcher<String> valueMatcher)
      Constructor, best called from one of the hasXPath static factory methods.
      Parameters:
      xPathExpression - the target xpath
      namespaceContext - the namespace for matching nodes
      valueMatcher - matcher for the expected value
  • Method Details

    • matchesSafely

      public boolean matchesSafely(Node item, Description mismatch)
      Description copied from class: TypeSafeDiagnosingMatcher
      Subclasses should implement this. The item will already have been checked for the specific type and will never be null.
      Specified by:
      matchesSafely in class TypeSafeDiagnosingMatcher<Node>
      Parameters:
      item - the item.
      mismatch - the mismatch description.
      Returns:
      boolean true/false depending if item matches matcher.
    • describeTo

      public void describeTo(Description description)
      Description copied from interface: SelfDescribing
      Generates a description of the object. The description may be part of a description of a larger object of which this is just a component, so it should be worded appropriately.
      Parameters:
      description - The description to be built or appended to.
    • hasXPath

      public static Matcher<Node> hasXPath(String xPath, Matcher<String> valueMatcher)
      Creates a matcher of Nodes that matches when the examined node has a value at the specified xPath that satisfies the specified valueMatcher. For example:
      assertThat(xml, hasXPath("/root/something[2]/cheese", equalTo("Cheddar")))
      Parameters:
      xPath - the target xpath
      valueMatcher - matcher for the value at the specified xpath
      Returns:
      The matcher.
    • hasXPath

      public static Matcher<Node> hasXPath(String xPath, NamespaceContext namespaceContext, Matcher<String> valueMatcher)
      Creates a matcher of Nodes that matches when the examined node has a value at the specified xPath, within the specified namespaceContext, that satisfies the specified valueMatcher. For example:
      assertThat(xml, hasXPath("/root/something[2]/cheese", myNs, equalTo("Cheddar")))
      Parameters:
      xPath - the target xpath
      namespaceContext - the namespace for matching nodes
      valueMatcher - matcher for the value at the specified xpath
      Returns:
      The matcher.
    • hasXPath

      public static Matcher<Node> hasXPath(String xPath)
      Creates a matcher of Nodes that matches when the examined node contains a node at the specified xPath, with any content. For example:
      assertThat(xml, hasXPath("/root/something[2]/cheese"))
      Parameters:
      xPath - the target xpath
      Returns:
      The matcher.
    • hasXPath

      public static Matcher<Node> hasXPath(String xPath, NamespaceContext namespaceContext)
      Creates a matcher of Nodes that matches when the examined node contains a node at the specified xPath within the specified namespace context, with any content. For example:
      assertThat(xml, hasXPath("/root/something[2]/cheese", myNs))
      Parameters:
      xPath - the target xpath
      namespaceContext - the namespace for matching nodes
      Returns:
      The matcher.