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
sandeepathadusandeepathadu 

how to cover the else conditions in test case

this is my class

public class Oppctrldelete
{
    decimal j = 0;

    public Oppctrldelete(ApexPages.StandardController controller) {


this.id =ApexPages.currentPage().getParameters().get('id');
op =[select id,name,Total_Invoiced__c,Total_Approved__c ,Billings_Indicator__c ,StageFilter__c,final_stage__c ,Quote_vs_Billings_Difference__c ,Filterstage__c,Total_Planned__c,Total_Submitted__c,ownerid,Purchase_order__c,Account.name,stagename,CloseDate,amount,probability,Header_Opportunity__c,Accountid,Opp_Line_Items_Count__c,Billing_Contact__c,No_Of_Months_for_Billing__c from Opportunity where id=:id];

oli = [select id,Quantity,delete__c ,description,pricebookentry.name,UnitPrice,TotalPrice ,Opportunityid,Quote_Original_List_Price__c,Product_Code__c   from OpportunityLineItem where opportunityid=:id ORDER BY Quantity];
if(op.StageFilter__c== 'Billing-Planned'){
opps =[select id,ownerid,Final_Stage__c,name,Account.name,stagename,CloseDate,amount,probability,Header_Opportunity__c,Accountid,Opp_Line_Items_Count__c,Billing_Contact__c,No_Of_Months_for_Billing__c,Filterstage__c from Opportunity where Header_Opportunity__c =:op.id ORDER BY closedate];
oppli = [select id,Line_Item_Total__c,Quantity,Quantity__c,PricebookEntryId,delete__c ,TotalPrice ,description,UnitPrice,Opportunityid,Quote_Original_List_Price__c,Product_Code__c   from OpportunityLineItem where opportunityid in: opps ORDER BY Quantity];
}
else if(op.StageFilter__c== 'Billing-Submitted'){
opps =[select id,ownerid,Final_Stage__c,name,Account.name,stagename,CloseDate,amount,probability,Header_Opportunity__c,Accountid,Opp_Line_Items_Count__c,Billing_Contact__c,No_Of_Months_for_Billing__c,Filterstage__c from Opportunity where Header_Opportunity__c =:op.id AND (stagename='Billing-Submitted' OR  stagename='Billing-Approved' OR  stagename='Billing-Invoiced') ORDER BY closedate];
oppli = [select id,Line_Item_Total__c,Quantity,Quantity__c,delete__c ,PricebookEntryId,TotalPrice ,description,UnitPrice,Opportunityid,Quote_Original_List_Price__c,Product_Code__c   from OpportunityLineItem where opportunityid in: opps ORDER BY Quantity];
}
else if(op.StageFilter__c== 'Billing-Approved'){
opps =[select id,ownerid,Final_Stage__c,name,Account.name,stagename,CloseDate,amount,probability,Header_Opportunity__c,Accountid,Opp_Line_Items_Count__c,Billing_Contact__c,No_Of_Months_for_Billing__c,Filterstage__c from Opportunity where Header_Opportunity__c =:op.id  AND  (stagename='Billing-Approved' OR stagename='Billing-Invoiced') ORDER BY closedate];
oppli = [select id,Line_Item_Total__c,PricebookEntryId ,Quantity,Quantity__c,delete__c ,TotalPrice ,description,UnitPrice,Opportunityid,Quote_Original_List_Price__c,Product_Code__c   from OpportunityLineItem where opportunityid in: opps ORDER BY Quantity];
}
else if(op.StageFilter__c== 'Billing-Invoiced'){
opps =[select id,ownerid,Final_Stage__c,name,Account.name,stagename,CloseDate,amount,probability,Header_Opportunity__c,Accountid,Opp_Line_Items_Count__c,Billing_Contact__c,No_Of_Months_for_Billing__c,Filterstage__c from Opportunity where Header_Opportunity__c =:op.id   AND  stagename='Billing-Invoiced' ORDER BY closedate];
oppli = [select id,Line_Item_Total__c,PricebookEntryId,Quantity,Quantity__c,delete__c ,TotalPrice ,description,UnitPrice,Opportunityid,Quote_Original_List_Price__c,Product_Code__c   from OpportunityLineItem where opportunityid in: opps ORDER BY Quantity];
}

 //opadd =[select id, MAX(CloseDate)   from Opportunity Limit 1 ];
//oppli = [select id,Line_Item_Total__c,Quantity,delete__c ,TotalPrice ,description,UnitPrice,Opportunityid,Quote_Original_List_Price__c,Product_Code__c   from OpportunityLineItem where opportunityid in: opps];
  Oppsnew = new List<Opportunity>();
  Opplinew = new List<Opportunitylineitem>();
   

   
    }
// Save The Updates on Opp and LineIems
   
    
   
    public PageReference save()    
     {
     try
     {     
   
         update oppli;
         oppli[0].Fire_Trigger__c = true;
         update Opps;
         //oli[0].Fire_Trigger__c = true;
         update oli;        
         update op;
       }
       
     catch(DmlException ex)
    {
        ApexPages.addMessages(ex);
        return null;
    }     
   return(new ApexPages.StandardController(Op)).view();  
    }


  // Cancel and go Back to Header Opportunity
   public PageReference cancel() 
    {
     return(new ApexPages.StandardController(Op)).view();
        return null;
    }
    
    
    public PageReference addmonthdyn()
     { 
    try
     {
     integer d;
     integer lkj = opps.size();
     integer poi = lkj-1;
 
     OpportunitylineItem[] oli = [select id,TotalPrice,ListPrice,PricebookEntry.name,Quantity,PricebookEntry.Product2.Name , PricebookEntry.Product2.id, UnitPrice,PricebookEntryId,Opportunityid,Quote_Original_List_Price__c  from OpportunityLineItem where Opportunityid=:id ORDER BY Quantity] ;
     opps[poi] =  [select id,Final_Stage__c ,Final_Closed_Date__c,stagename,Description,AccountId ,Hold_My_date__c,Week_End_Day__c ,name,closedate,Estimated_Start_Date__c from Opportunity where Header_Opportunity__c=:op.id ORDER BY closedate DESC LIMIT 1];
   
     Opportunity newopp= new Opportunity();
     newopp.stagename = 'Billing-Planned';
     newopp.Header_Opportunity__c=op.id;     
     newopp.recordtypeid='012M000000008mu';
     string mon;    
       
     decimal x = (poi+1*30);     
     newopp.Hold_My_date__c = opps[poi].closedate +x.intValue();
    
    // LitOrd.closedate = o.closedate;
    // for(Opportunity opps2:opps)
      //{
       //LitOrd.closedate = opps2.closedate+60; 
      // LitOrd.closedate = LitOrd.closedate+30; 
    // }
     newopp.closedate = opps[poi].closedate+30; 
    //LitOrd.closedate = opps[poi].closedate;   
   // Integer currentyear = opps[poi].Hold_My_date__c.year();
   // Integer currentMonth = (opps[poi].Hold_My_date__c.Month())+1;
       Integer currentyear =  newopp.Hold_My_date__c.year();
       Integer currentMonth =  newopp.Hold_My_date__c.Month();
        if(currentMonth==1){
          mon = 'JAN';
          
         
         }
         else if(currentMonth==2){
         mon = 'FEB';
         }
          else if(currentMonth==3){
         mon = 'MAR';
         }
          else if(currentMonth==4){
         mon = 'APR';
         }
          else if(currentMonth==5){
         mon = 'MAY';
         }
          else if(currentMonth==6){
         mon = 'JUN';
         }
          else if(currentMonth==7){
         mon = 'JUL';
         }
          else if(currentMonth==8){
         mon = 'AUG';
         }
          else if(currentMonth==9){
         mon = 'SEP';
         }
          else if(currentMonth==10){
         mon = 'OCT';
         }
          else if(currentMonth==11){
         mon = 'NOV';
         }
          else if(currentMonth==12){
         mon = 'DEC';
         }
        
      newopp.accountid = opps[poi].accountid;
      newopp.name = op.Account.name+'-'+op.Name+'-'+'Billing'+'-'+mon+'-'+ currentyear;
      
      newopp.final_stage__c ='Billing-Planned';
      newopp.probability =100;
      newopp.amount = 100;
     
      Oppsnew.add(newopp); 
      insert oppsnew;
    
    
   
  }
   catch(DmlException ex)
   {
     ApexPages.addMessages(ex);
     return null;
   }
   PageReference customPage = new PageReference('/apex/opptctldelete'); 
   customPage.getParameters().put('id', this.id);
   customPage.setRedirect(true);
   return customPage;
  
    }
    
   
    
   //Quick save changes
   public PageReference quicksave()
   {
     try
     {    
    

        update oppli;
        oppli[0].Fire_Trigger__c = true;
        update Opps;
        //oli[0].Fire_Trigger__c = true;
        update oli;        
        update op;
       
     
    
    }
    catch(DmlException ex)
    {
        ApexPages.addMessages(ex);
        return null;
    }
     
   PageReference customPage = new PageReference('/apex/opptctldelete');   
   customPage.getParameters().put('id', this.id);
   customPage.setRedirect(true);
   return customPage;             
    }   

  
    
   public List<Opportunity> Oppsnew  {get; set;} 
   public List<Opportunitylineitem> Opplinew  {get; set;} 
   public Opportunity Op ;
   public Opportunity opadd;
   public Opportunity[] Opps ;
   public OpportunitylineItem[] Oppli ;
   public OpportunitylineItem[] Oli ;
   public string id;
   decimal gtotal=0.0;
   decimal itotal=0;
   user u;
   decimal countindvidual;
   decimal actualcountindvidual;  
   decimal BItotal;
   decimal count = 0; 
   decimal finalBItotal =0;
   decimal counttotal = 0;
   decimal  totval;
   decimal childtot=0.0;

public String getname()
{
return 'Oppctrldelete';
}
public Opportunity getop()
{
return op;
}
public user getu()
{
return u;
}
public Opportunity [] getopps()
{

return opps;
}


 public PageReference filterstage()
  {
 update op;
   
  PageReference customPage = new PageReference('/apex/opptctldelete');   
  customPage.getParameters().put('id', this.id);
  customPage.setRedirect(true);
  return customPage; 
            
    }   



public OpportunitylineItem[] getoppli()
{

return oppli;
}
public OpportunitylineItem[] getoli()
{

return oli;
}

 
 
  public decimal gettotval()
 {
return totval;
}
public decimal getcountindvidual() {
return countindvidual;
}
 public decimal getgtotal()
 {
return gtotal;
} 
public decimal getBItotal()
 {
return BItotal;
} 
}
this is my test case giving 79 percent code coverage and the else conditions are not getting covered plz help me to write test case in easiest possible way
@isTest
private class Oppctrldelete_Test
{
static testmethod void MyTestController()
 {

        Pricebook2 standardPB = [select id from Pricebook2 where isStandard=true];

        Pricebook2 pb = new Pricebook2(Name = 'Standard Price Book 2009', Description = 'Price Book 2009 Products', IsActive = true);
        insert pb;
        
        Product2 prod = new Product2(Name = 'Anti-infectives 2007', Family = 'Best Practices', IsActive = true);
        insert prod;

        PricebookEntry standardPrice = new PricebookEntry(Pricebook2Id = standardPB.Id, Product2Id = prod.Id, UnitPrice = 10000, IsActive = true, UseStandardPrice = false);
        insert standardPrice;

        PricebookEntry pbe = new PricebookEntry(Pricebook2Id = pb.Id, Product2Id = prod.Id, UnitPrice = 10000, IsActive = true, UseStandardPrice = false);
        insert pbe;
         
 Account a = new Account();
 a.name = 'raja';
 insert a ;
 
  
 Opportunity opp = new Opportunity();
 opp.Name = 'raja';
 opp.StageName = 'Closedwon';
 opp.final_stage__c= 'Billing-Planned';
 opp.StageFilter__c = 'Billing-Submitted';
 
 opp.StageFilter__c = 'Billing-Planned';
 
 
 opp.CloseDate = date.ValueOf('2009-09-21');
 opp.Pricebook2Id = pb.id;
 opp.final_stage__c ='Billing-Invoiced';
 
 
 test.startTest();

 insert opp;
   
   test.stopTest();

   
   
   
   
   
   
     OpportunitylineItem opli = new OpportunitylineItem();
  opli.opportunityid=opp.id;
  opli.TotalPrice=5;
  opli.quantity=3;
  opli.PricebookEntryId = pbe.id; 
  
 insert opli;
     
   
   

      
 Opportunity opp1 = new Opportunity();
 opp1.Name = 'raj';
 opp1.StageName = 'Closedwon';
 opp1.StageFilter__c= 'Billing-Planned';
 opp1.CloseDate = date.ValueOf('2009-07-21');
 opp1.final_stage__c= 'Billing-Planned';
 opp1.Pricebook2Id = pb.id;
 opp1.header_opportunity__c = opp.id;
 insert opp1;
 
 
 
 
 
  
  OpportunitylineItem opli1 = new OpportunitylineItem();
  opli1.opportunityid=opp1.id;
  opli1.TotalPrice=56;
  opli1.quantity=33;
  opli1.PricebookEntryId = pbe.id; 
 opli1.delete__c=true;
  insert opli1;
  update opli;
    update opli1;
    
    
    
    
       
 ApexPages.currentPage().getParameters().put('id', opp.id);

 
 
 
  
 ApexPages.StandardController con = new ApexPages.StandardController(opp);
 Oppctrldelete o = new Oppctrldelete(con);
 
 
 PageReference pr2 = o.save();
 PageReference pr3 = o.addmonthdyn();
 PageReference pr4 = o.quicksave();
//  PageReference pr5 = o.testsave();
 o.getname();
 o.getopps();
 o.filterstage();
 o.getoppli();
 o.getop();
 O.getu();
 o.getoli();
 o.getcountindvidual();
 o.getgtotal();
 o.getBItotal();
 o.gettotval();
 o.cancel();
 //o.Addmonth();
// o.incrementCounter();
// o.quan();
 //o.childtotal();
// o.BI();
 //o.grandtotal();
  
  
 
 
 
  }
 }

 

 

raseshtcsraseshtcs

You might want to insert another opportunity like you have in your test method, change the op.StageFilter__c field value for each opportunitya and insert the opportunities separately e.g opp2. Then call the methods again using this instance.

 

 

ApexPages.StandardController con1 = new ApexPages.StandardController(opp2); 
Oppctrldelete o1 = new Oppctrldelete(con1);