### Question:
XPathExpression implements Expression { @Override public ExpressionResult apply(ExpressionContext expressionContext, ExpressionResult input) { Object doc = document.apply(expressionContext, input).asXml(); String xpathValue = xpath.apply(expressionContext, input).getValue().toString(); XPath xpathEvaluator = xpathFactory.newXPath(); Object result = null; ExpressionResult outputTypeValue = null; try { if( outputType != null) { outputTypeValue = outputType.apply(expressionContext, input); if( doc instanceof InputSource) { result = xpathEvaluator.compile(xpathValue).evaluate( (InputSource)doc, outputType(outputTypeValue)); } else { result = xpathEvaluator.compile(xpathValue).evaluate( doc, outputType(outputTypeValue)); } } else { if( doc instanceof InputSource) { result = xpathEvaluator.compile(xpathValue).evaluate((InputSource) doc ); } else { result = xpathEvaluator.compile(xpathValue).evaluate(doc); } } } catch (XPathExpressionException e) { throw new RuntimeException(e.getMessage(), e); } List<ExpressionResult> values = new ArrayList<>(); if( result instanceof Element) { return toExpressionResult(result, outputTypeValue); } if( result instanceof NodeList) { NodeList resultArray = (NodeList)result; for( int i = 0; i < resultArray.getLength(); i++) { values.add(toExpressionResult(resultArray.item(i), outputTypeValue)); } return new ListExpressionResult(values); } return toExpressionResult(result, outputTypeValue); } XPathExpression(String alias, Expression[] expressions); @Override ExpressionResult apply(ExpressionContext expressionContext, ExpressionResult input); @Override String alias();  }### Answer:
@Test public void apply() { ExpressionResult result = testSubject.apply(expressionContext, mapValue("test", stringValue(xmlDocument), "expression", stringValue(" assertThat(result).isInstanceOf(ListExpressionResult.class); ListExpressionResult resultList = (ListExpressionResult) result; assertThat(resultList.getValue()).contains(new StringExpressionResult("Second")); }
@Test public void applyWithNodes() { ExpressionResult result = testSubjectWithoutType.apply(expressionContext, mapValue("test", stringValue(xmlDocument), "expression", stringValue("count( assertThat(result).isInstanceOf(StringExpressionResult.class); assertThat(result.toString()).isEqualTo("4"); }
@Test public void applyCount() { ExpressionResult result = testSubject.apply(expressionContext, mapValue("test", stringValue(xmlDocument), "expression", stringValue("count( assertThat(result).isInstanceOf(NumericExpressionResult.class); assertThat(result.getNumericValue().intValue()).isEqualTo(4); }
@Test public void getIndex() { ExpressionResult result = testSubjectWithoutType.apply(expressionContext, mapValue("test", stringValue(xmlDocument), "expression", stringValue(" assertThat(result).isInstanceOf(StringExpressionResult.class); assertThat(result.toString()).isEqualTo("12692"); }
@Test public void getIndexNode() { ExpressionResult result = testSubject.apply(expressionContext, mapValue("test", stringValue(xmlDocument), "expression", stringValue(" "resultType", stringValue("node"))); assertThat(result).isInstanceOf(XmlExpressionResult.class); assertThat(result.toString()).isEqualTo("<index>12692</index>"); }