java - Can't create List<Node> object when list contains one element -


i'm using rest-assured 2.8.0 library in java tests. when create xmlpath rest-assured string response , try create list of nodes exception if list contains 1 element.

list<node> nodelist = xmlpath.getlist("commentary.messages.period.message", node.class); 

this should give me list 1 element instead exception

cannot convert class java.lang.string interface com.jayway.restassured.path.xml.element.node. java.lang.classcastexception @ sun.reflect.nativeconstructoraccessorimpl.newinstance0(native method) @ sun.reflect.nativeconstructoraccessorimpl.newinstance(nativeconstructoraccessorimpl.java:62) @ sun.reflect.delegatingconstructoraccessorimpl.newinstance(delegatingconstructoraccessorimpl.java:45) @ java.lang.reflect.constructor.newinstance(constructor.java:408) @ org.codehaus.groovy.reflection.cachedconstructor.invoke(cachedconstructor.java:80) @ org.codehaus.groovy.runtime.callsite.constructorsite$constructorsitenounwrapnocoerce.callconstructor(constructorsite.java:105) @ org.codehaus.groovy.runtime.callsite.callsitearray.defaultcallconstructor(callsitearray.java:60) @ org.codehaus.groovy.runtime.callsite.abstractcallsite.callconstructor(abstractcallsite.java:235) @ org.codehaus.groovy.runtime.callsite.abstractcallsite.callconstructor(abstractcallsite.java:247) @ com.jayway.restassured.internal.path.objectconverter.convertobjectto(objectconverter.groovy:54) @ com.jayway.restassured.path.xml.xmlpath.convertobjectto(xmlpath.java:913) @ com.jayway.restassured.path.xml.xmlpath.getaslist(xmlpath.java:879) @ com.jayway.restassured.path.xml.xmlpath.getlist(xmlpath.java:348) @ com.performgroup.usbasketballbackend.sprint26.usbask382test.checkplayermaineventwithqualifiermessagescomments(usbask382test.java:125)  

when have 2 elements not exception. please :)

below xml structure , if there 1 message node doesn't create list 1 element.

<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> 

but if have 2 message nodes creates list 2 elements.

<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> 

this happens because of odd behavior of groovy java conversion in in xml-path-2.8.0-sources.jar!/com/jayway/restassured/assertion/xmlassertion.groovy

look what's happening:

private def tojavaobject(nodes, isattributes, forcelist) {     if (nodes.size() == 1 && !haschildren(nodes, isattributes)) {         return nodes.text()     } else {         return tojavalist(nodes, isattributes, forcelist)     } } 

if list of nodes consists of 1 element no children, element has no rights node according implementation. it's converted string. triggers classcastexception in code later on, you're expecting node , getting string.

so getting list of nodes seems broken. can't use it. have find workaround suites test scenario. example, can access attributes list of strings:

list<string> nodelist = xmlpath.getlist("commentary.messages.period.message.@id", string.class); 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -