function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
AkiTAkiT 

How to write tests for xmlStreamWriter

Code:
global class XmlWriterCamos {
 
 WebService static String getXmlStream(Id[] versId) {

//INITIATE XML STREAM
  XmlStreamWriter w = new XmlStreamWriter();

//INPUT START ELEMENT
        w.writeStartElement(null, 'Data', null);
        
        //INPUT CurrentUser INFO
        w.writeStartElement(null, 'CurrentUser', null);
        w.writeStartElement(null, 'ID', null);
        w.writeCharacters(u[0].Id);
        w.writeEndElement(); //end Id
        if(u[0].City != null){
            w.writeStartElement(null, 'City', null);
            w.writeCharacters(u[0].City);
            w.writeEndElement(); //end City
        }
...

Quick shoot:
 
How would you write unit tests for such a code?

jlojlo
Pseudo-code:
string EXPECTED_XML = '...';
string actualXml = getXmlStream(versId); System.assertEquals(EXPECTED_XML, actualXml);
 
lopezclopezc
Hi,
 
Thanks for you code! it helped me a lot. But I have a small question about the return param in the first piece of code.. if you have a XmlStreamWriter object, how do you return a String? I tried with w.toString() but it is not working.. any help?
 
Thanks!
AkiTAkiT
XmlStreamWriter class has its own methods.


Code:
String xmlOutput = w.getXmlString();