我在Java测试中使用了放心的2.8.0库。当我从保证的String响应创建一个xmlPath并尝试创建一个节点列表时,如果List只包含一个元素,我会得到一个异常。
List<Node> nodeList = xmlPath.getList("commentary.messages.period.message", Node.class);
这应该给我一个只包含一个元素的List,但我得到一个例外
Cannot convert class java.lang.String to interface com.jayway.restassured.path.xml.element.Node. java.lang.ClassCastException at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:408) at org.codehaus.groovy.reflection.CachedConstructor.invoke(CachedConstructor.java:80) at org.codehaus.groovy.runtime.callsite.ConstructorSite$ConstructorSiteNoUnwrapNoCoerce.callConstructor(ConstructorSite.java:105) at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallConstructor(CallSiteArray.java:60) at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:235) at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:247) at com.jayway.restassured.internal.path.ObjectConverter.convertObjectTo(ObjectConverter.groovy:54) at com.jayway.restassured.path.xml.XmlPath.convertObjectTo(XmlPath.java:913) at com.jayway.restassured.path.xml.XmlPath.getAsList(XmlPath.java:879) at com.jayway.restassured.path.xml.XmlPath.getList(XmlPath.java:348) at com.performgroup.usbasketballbackend.sprint26.USBask382Test.checkPlayerMainEventWithQualifierMessagesComments(USBask382Test.java:125)
当我有2个元素时,我没有得到这个例外。请帮助:)
下面是xml结构,如果有一个消息节点,它不会创建一个包含一个元素的列表。
<commentary xmlns:basketballdata="www.nba.com">
<matchInfo>
<description>Oklahoma City Thunder vs Houston Rockets</description>
<sport id="456">basketball</sport>
<competition id="1" name="NBA">
<country id="123" name="USA"/>
</competition>
</matchInfo>
<messages>
<period id="1">
<message id="123" homeScore="0" awayScore="0"/>
</period>
</messages>
</commentary>
但如果我有2个消息节点,它会创建一个包含2个元素的列表。
<commentary xmlns:basketballdata="www.nba.com">
<matchInfo>
<description>Oklahoma City Thunder vs Houston Rockets</description>
<sport id="456">basketball</sport>
<competition id="1" name="NBA">
<country id="123" name="USA"/>
</competition>
</matchInfo>
<messages>
<period id="1">
<message id="123" homeScore="0" awayScore="0"/>
<message id="456" homeScore="2" awayScore="0"/>
</period>
</messages>
</commentary>
答案 0 :(得分:0)
这是因为xml-path-2.8.0-sources.jar!/com/jayway/restassured/assertion/XMLAssertion.groovy
看看发生了什么:
private def toJavaObject(nodes, isAttributes, forceList) {
if (nodes.size() == 1 && !hasChildren(nodes, isAttributes)) {
return nodes.text()
} else {
return toJavaList(nodes, isAttributes, forceList)
}
}
如果节点列表只包含一个没有子节点的元素,则此元素无权成为根据此实现的节点。它已转换为String
。这会在您的代码中触发ClassCastException
,因为您期待Node
并获得String
。
因此获得节点列表似乎已被打破。你不能使用它。您必须找到适合您的测试场景的解决方法。例如,您可以作为字符串列表访问属性:
List<String> nodeList = xmlPath.getList("commentary.messages.period.message.@id", String.class);