[SalesForce] Help with Code coverage, Loop and if conditions

I'm unable to have code coverage. Wrote a test class that has 24% coverage. How should I test loops and conditional statements?

Following is my Apex Class…

public class OrderTrackingList{
public OrderTrackingList(ApexPages.StandardController controller) {}
public list<wrapgroupwise> singlegroup;
public List<wrapgroupwise> getStartHere(){
singlegroup= new List<wrapgroupwise>();
List<orders__c> tempacc=[SELECT Id,Name FROM orders__c where id=:ApexPages.currentPage().getParameters().get('id')];

 for(Integer i=0; i<tempacc.size() ; i++){
    List <order_lines__c> tempOrderLines=[SELECT Id,name From order_lines__c Where order_number__c=:tempacc[i].id];
    List <shipment_lines__c> tempShipmentLines=[SELECT Id,Name From shipment_lines__c Where Order_Number__c=:tempacc[i].id];

  if(tempOrderLines.size()==0 && tempShipmentLines.size()==0){
          singlegroup.add(new wrapgroupwise(tempacc[i],null,null)); 
    }
    else{

     if(tempShipmentLines.size()==0)
     {
          if(tempOrderLines[i].status__c=='Open')
           {
               tempOrderLines[i].calculated_estimated_delivery_date__c=tempOrderLines[i].planned_ship_date__c;
           }

        singlegroup.add(new wrapgroupwise(tempacc[i],tempOrderLines,null)); 

      }   
     else
     {

       if(tempOrderLines[i].status__c=='Open')
       {
           tempOrderLines[i].calculated_estimated_delivery_date__c=tempOrderLines[i].planned_ship_date__c;
       }
       if(tempOrderLines[i].status__c=='Invoiced')
       {
           tempOrderLines[i].calculated_estimated_delivery_date__c=tempShipmentLines[i].estimated_delivery_date__c;
       }
       if(tempOrderLines[i].status__c=='Shipped but not billed')
       {
           tempOrderLines[i].calculated_estimated_delivery_date__c=tempShipmentLines[i].estimated_delivery_date__c;
       }           

      singlegroup.add( new wrapgroupwise(tempacc[i],tempOrderLines,tempShipmentLines)); 
     }

    }
   }
  return singlegroup; 
  }
 public class wrapgroupwise
 {
    public List<order_lines__c> con {get;set;}
    public orders__c acc {get;set;}
    public List<shipment_lines__c> opp {get;set;}

     public wrapgroupwise( orders__c a , list<order_lines__c> c,list<shipment_lines__c> o)
     {
        acc=a;
        con=c;
        opp=o;
      } 
  } 
 }

========================TEST CLASS===========================

 @isTest
 public class OrderTrackingList_Test{ 
static testMethod void coverCode(){

          //OrderTrackingList.wrapgroupwise wgw = new            OrderTrackingList.wrapgroupwise();
          Profile pf= [SELECT Id FROM Profile WHERE name= 'System Administrator' LIMIT 1];

          Account acc = new Account(Name='ABC Corp.');
          insert acc;
          System.assertEquals('ABC Corp.', acc.name);

          orders__c tempOrder = new orders__c();
          tempOrder.name = '0001234567';
          tempOrder.account__c = acc.id;
          insert tempOrder;
          System.assertEquals('0001234567', tempOrder.name);


          order_lines__c tempOL = new order_lines__c();
          tempOL.Name = '10';
          tempOL.number_of_order_lines__c = tempOrder.id;
          insert tempOL;
          System.assertEquals('10', tempOL.name);

          shipment_lines__c tempSL = new shipment_lines__c();
          tempSL.Name = '10'; 
          tempSL.CurrencyIsoCode = 'USD'; 
          insert tempSL;

          System.assertEquals('USD', tempSL.CurrencyIsoCode);

         OrderTrackingList gepwO = new OrderTrackingList(new ApexPages.StandardController(tempOrder));  
         OrderTrackingList gepwOL = new OrderTrackingList(new ApexPages.StandardController(tempOL));
         OrderTrackingList gepwSL = new OrderTrackingList(new ApexPages.StandardController(tempSL));
         gepwO.getStartHere();
         //gepwOL.singlegroup.add(gepwOL);
        // gepwSL.singlegroup.add(gepwSL);
        // gepwO.singlegroup.add(gepwO);
        }
   }

What am I missing? Please guide.
Thanks.

Best Answer

How should I test loops and conditional statements?

Your test methods need to create the data which meets your conditions.

What am I missing?

This line of code will require your unit test to set the current page reference and provide an ID value in the parameters collection. Without it, there is no "current page", no id available and thus no parameter for the query.

List<orders__c> tempacc=[SELECT Id,Name 
 FROM orders__c where id=:ApexPages.currentPage().getParameters().get('id')];

Similar to this: (SFDC Docs on testing controllers and extensions)

PageReference pageRef = Page.YourPageName;
pageRef.getParameters().put('id', theIdOfYourOrder);
Test.setCurrentPage(pageRef);

Your unit test might contain this near the end after you've inserted all of your data:

Test.startTest();

PageReference pageRef = Page.YourPageName;
pageRef.getParameters().put('id', tempOrder.Id);
Test.setCurrentPage(pageRef);

OrderTrackingList gepwO = new OrderTrackingList(new ApexPages.StandardController(tempOrder));  
gepwO.getStartHere();

/* your asserts for business requirements */

Test.stopTest();

These two lines are invalid given the code you've provided:

// OrderTrackingList gepwOL = new OrderTrackingList(new ApexPages.StandardController(tempOL));
// OrderTrackingList gepwSL = new OrderTrackingList(new ApexPages.StandardController(tempSL));

As an aside, the assert statements in the test class which you've posted are of very little value. (Unless you're concerned with ensuring that the Name field didn't get manipulated by another process outside of the test, which is probably not the case.)

It's great that there are assert statements in the code, but they should assert the business requirements that the custom code was written to address. This ensures that the custom code is doing what it is supposed to be doing.

gew_orders__c tempOrder = new gew_orders__c();
tempOrder.name = '0001234567';
tempOrder.gew_account__c = acc.id;
insert tempOrder;
System.assertEquals('0001234567', tempOrder.name);
Related Topic