title: Mockito: How to mock XML Document object to retrieve value from XPath
body: <p>I am trying to mock a org.w3c.dom.Document object such that calling XPath.evaluate() should return a defined value, e.g., foo, as below: </p>
  
  <pre><code>Document doc = Mockito.mock(Document.class);    
  Mockito.when(XPathUtils.xpath.evaluate("/MyNode/text()", doc, XPathConstants.STRING)).thenReturn("foo");
  </code></pre>
  
  <p>I shall pass the doc object to the target method that will extract the textual contents of node <strong>MyNode</strong> as <strong>foo</strong>. </p>
  
  <p>I have tried mocking nodes and setting in the doc object as follows:</p>
  
  <pre><code>            Node nodeMock = mock(Node.class);
              NodeList list = Mockito.mock(NodeList.class);
              Element element = Mockito.mock(Element.class);
              Mockito.when(list.getLength()).thenReturn(1);
              Mockito.when(list.item(0)).thenReturn(nodeMock);
              Mockito.when(doc.getNodeType()).thenReturn(Node.DOCUMENT_NODE);
              Mockito.when(element.getNodeType()).thenReturn(Node.ELEMENT_NODE);
              Mockito.when(nodeMock.getNodeType()).thenReturn(Node.TEXT_NODE);
              Mockito.when(doc.hasChildNodes()).thenReturn(false);
              Mockito.when(element.hasChildNodes()).thenReturn(true);
              Mockito.when(nodeMock.hasChildNodes()).thenReturn(false);
              Mockito.when(nodeMock.getNodeName()).thenReturn("MyNode");
              Mockito.when(nodeMock.getTextContent()).thenReturn("MyValue");
              Mockito.when(element.getChildNodes()).thenReturn(list);
              Mockito.when(doc.getDocumentElement()).thenReturn(element);
  </code></pre>
  
  <p>But this is giving error like:</p>
  
  <blockquote>
    <p>org.mockito.exceptions.misusing.WrongTypeOfReturnValue:  String cannot
    be returned by hasChildNodes() hasChildNodes() should return boolean</p>
  </blockquote>
  
  <p>Is my approach correct and I am missing just another mock, or should I approach it differently? Please help.</p>