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
jhansisridhar_2011jhansisridhar_2011 

Help to increase code coverage !!!

Hi all,

 

Please help me to increase code coverage in test class, I have written a apexclass,

 
 public void storeProductsTxt(){
        System.debug('********** Store Products in XML **********');        
        XmlStreamWriter w = new XmlStreamWriter();
        w.writeStartDocument(null, '1.0');
        w.writeStartElement(null, 'Products', null);
        
        String output = '';
        
        for (Product2 p : [Select p.Description, p.Family, p.Id, p.IsActive, p.Name, p.PS_Product_SKU__c, p.Strategic_Value__c, p.Unit_of_Measure__c, 
            p.Maximum_Order_Qty__c, p.Multiple_Order_Qty__c, 
            (Select Name, Quantity_in_Stock__c, Ship_From__c From Inventory__r) 
            from Product2 p where isActive = true limit 10]){
                
            output = '<Product><Id>' + p.Id + '</Id><Name>' + p.Name + '</Name><Description>' + p.Description + '</Description><PSProductSKU>' + p.PS_Product_SKU__c + '</PS Product SKU><UOM>' + p.Unit_of_Measure__c + '</UOM><MAX>' + String.valueOf(p.Maximum_Order_Qty__c) + '</MAX><Multiple>' + String.valueOf(p.Multiple_Order_Qty__c) + '</Multiple><Inventories>';
            
            for (Inventory__c inv : p.Inventory__r){
                output += '<Inventory><WH>' + inv.Ship_From__c + '</WH><Stock>' + String.valueOf(inv.Quantity_In_Stock__c) + '</Stock></Inventory>';    
            }
            
            output += '</Inventories></Product>';
            
            w.writeCData(output);
            
            //System.debug('p: ' + p);
            //output = p + '\n';
            //b = Blob.valueOf(output); 
            
        }
        w.writeEndElement();
        w.writeEndDocument();
        
        String xmlOutput = w.getXmlString();
        System.debug('*** XML: ' + xmlOutput);
        w.close();
        //System.debug('--> output: ' + output);
        
        //Blob b = Blob.valueof(xmlOutput);
        Blob b = Blob.valueof('text');
        
        List<Attachment> list_attachments = [Select Body from Attachment where Name = 'ProductInfo.xml' and ParentId = '001T000000F7raXIAR' limit 1000];
        if (list_attachments.size() > 0){
            delete list_attachments;
        }
        
        Attachment a = new Attachment(); 
        a.Name = 'ProductInfo.xml';
        a.Body = b;
        a.ParentId = '001T000000F7raXIAR';
        a.ContentType = 'text/xml';
        a.IsPrivate = false;
        insert a;
        
    }
getting error message as invalid cross reference id .

Thanks in advance.
MandyKoolMandyKool

Hi,

 

Generally you get cross reference id error; if you are setting up a wrong id in lookup field.

 

eg. If you place ContactID instead of AccountId on a field.

 

Also; I can see that you have used the hard-coded ID's in your test method. Its good practice to create your own records in test method and use them as the ids in 2 different instances may vary.

 

 

jhansisridhar_2011jhansisridhar_2011

Hi Kulkarni,

 

Thanks for reply, I realized the problem nd made necessary updates, it's working fine now.

I have written class where am using xmlstreamwriter , am not able to write a test class to cover 75% for below code

public void storeProducts(){
 231   System.debug('********** Store Products in XML **********');
 232   XmlStreamWriter w = new XmlStreamWriter();
 233   w.writeStartDocument(null, '1.0');
 234   w.writeStartElement(null, 'Products', null);
 235  
 236   for (Product2 p : [Select p.Description, p.Family, p.Id, p.IsActive, p.Name, p.PS_Product_SKU__c, p.Strategic_Value__c, p.Unit_of_Measure__c,
 237   p.Maximum_Order_Qty__c, p.Multiple_Order_Qty__c,
 238   (Select Name, Quantity_in_Stock__c, Ship_From__c From Inventory__r)
 239   from Product2 p where isActive = true limit 5000]){
 240  
 241   w.writeStartElement(null, 'Product', null);
 242   w.writeStartElement(null, 'Id', null);
 243   w.writeCharacters(p.Id);
 244   w.writeEndElement();
 245 
246   w.writeStartElement(null, 'Name', null);
 247   w.writeCharacters(p.Name);
 248   w.writeEndElement();
 249  
 250   w.writeStartElement(null, 'Description', null);
 251   w.writeCharacters(p.Description);
 252   w.writeEndElement();
 253  
 254   w.writeStartElement(null, 'PSProductSKU', null);
 255   w.writeCharacters(p.PS_Product_SKU__c);
 256   w.writeEndElement();
 257  
 258   w.writeStartElement(null, 'UOM', null);
 259   w.writeCharacters(p.Unit_of_Measure__c);
 260   w.writeEndElement();
 261  
 262   w.writeStartElement(null, 'MAX', null);
 263   w.writeCharacters(String.valueOf(p.Maximum_Order_Qty__c));
 264   w.writeEndElement();
 265  
 266   w.writeStartElement(null, 'Multiple', null);
 267   w.writeCharacters(String.valueOf(p.Multiple_Order_Qty__c));
 268   w.writeEndElement();
 269  
 270   w.writeStartElement(null, 'Inventories', null);
 271  
 272   for (Inventory__c inv : p.Inventory__r){
 273   w.writeStartElement(null, 'Inventory', null);
 274  
 275   w.writeStartElement(null, 'WH', null);
 276   w.writeCharacters(inv.Ship_From__c);
 277   w.writeEndElement();
 278  
 279   w.writeStartElement(null, 'Stock', null);
 280   w.writeCharacters(String.valueOf(inv.Quantity_in_Stock__c));
 281   w.writeEndElement();
 282  
 283   w.writeEndElement(); //end Inventory
 284   }
 285  
 286   w.writeEndElement(); // end Inventories
 287   w.writeEndElement(); //end Product
 288   }
 289  
 290   w.writeEndElement(); //end Products
 291   w.writeEndDocument();
 292  
 293   String xmlOutput = w.getXmlString();
 294   System.debug('*** XML: ' + xmlOutput);
 295   w.close();
 296  
 297   Blob b = Blob.valueof(xmlOutput);
 298  
 299   List<Attachment> list_attachments = [Select Body from Attachment where Name = 'ProductInfo.xml' and ParentId = '00190000009rvgY' limit 1000];
 300   if (list_attachments.size() > 0){
 301   delete list_attachments;
 302   }
 303  
 304   Attachment a = new Attachment();
 305   a.Name = 'ProductInfo.xml';
 306   a.Body = b;
 307   a.ParentId = '00190000009rvgY';
 308   a.ContentType = 'text/xml';
 309   a.IsPrivate = false;
 310   insert a;
 311  
 312   /* DML Operations not possible with SObject Static Resource !!!
 313   StaticResource sr = new StaticResource();
 314   sr.Name = 'ProductInfo';
 315   sr.CacheControl = 'Public';
 316   sr.Description = 'This XML File contains product information as well as according iventory information';
 317   sr.ContentType = 'text/xml';
 318   sr.Body = b;
 319   */
 320   }
jhansisridhar_2011jhansisridhar_2011

please help to write test class.

 

Thanks in Advance.