【发布时间】:2016-02-05 14:58:19
【问题描述】:
我在 Java 测试中使用放心的 2.8.0 库。当我从放心的字符串响应创建 xmlPath 并尝试创建节点列表时,如果列表仅包含一个元素,则会出现异常。
List<Node> nodeList = xmlPath.getList("commentary.messages.period.message", Node.class);
这应该给我一个只有一个元素的列表,但我得到一个异常
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>
【问题讨论】:
-
你能展示导致异常的数据样本和没有的数据样本吗?
-
我添加了一个示例 xml。我希望这会有所帮助。
-
我认为这是错误/功能,一个元素 - 不是列表,两个或更多 - 列表。捕获类转换异常并作为单个元素获取
标签: java rest-assured