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
Dev87Dev87 

apex test class - couvrage

Hello, 

I have this apex class:
public without sharing class Opp_CalculTotalForcast {

    public static void TotalForcast (List <Opportunity> oppList)
    {
        system.debug('debut: ' +oppList);  
        integer i;
        Decimal TotalForcastparc =0;
        Decimal TotalForcastcurrency = 0;
        Decimal TotalForcastDomPorc = 0;
        Decimal TotalForcastDomcurrency = 0;
        Decimal TotalForcastIntparc = 0;
        Decimal TotalForcastIntcurrency = 0;
        Integer Nb_BUs_participating = 1;
                
        list<Tender_BU_Response__c> allBUList = new list<Tender_BU_Response__c>();
        list<Opportunity> Opp = new list<Opportunity>();
       
        for(Opportunity o:oppList ){
          Id  Id= o.id;
         system.debug('idopp : ' +Id);  
            allBUList = [Select id,BU_Name__c,Total_Parcels_Forecast__c,Stage__c, Total_Revenues_Forecast__c, Domestic_Parcels_Forecast__c,Domestic_Revenues_Forecast__c,International_Parcels_Forecast__c,International_Revenues_Forecast__c    from Tender_BU_Response__c where Tender__c =:  Id];
                    system.debug('allBUList : ' +allBUList);  

            if (allBUList.size()>0)
           {
            for (i=0; i < allBUList.size(); i++)
             { 
               if (allBUList[i].Total_Parcels_Forecast__c != NULL)
               { TotalForcastparc = TotalForcastparc +allBUList[i].Total_Parcels_Forecast__c;}                             
               if (allBUList[i].Total_Revenues_Forecast__c != NULL)    
               {TotalForcastcurrency = TotalForcastcurrency +  allBUList[i].Total_Revenues_Forecast__c;}    
               if (allBUList[i].Domestic_Parcels_Forecast__c != NULL)
               {TotalForcastDomPorc = TotalForcastDomPorc +  allBUList[i].Domestic_Parcels_Forecast__c;} 
               if (allBUList[i].Domestic_Revenues_Forecast__c != NULL)
               {TotalForcastDomcurrency = TotalForcastDomcurrency + allBUList[i].Domestic_Revenues_Forecast__c;}
               if (allBUList[i].International_Parcels_Forecast__c != NULL)
               { TotalForcastIntparc = TotalForcastIntparc + allBUList[i].International_Parcels_Forecast__c;}
               if (allBUList[i].International_Revenues_Forecast__c != NULL)
               {TotalForcastIntcurrency = TotalForcastIntcurrency + allBUList[i].International_Revenues_Forecast__c;}
                
                 {Nb_BUs_participating = Nb_BUs_participating+i;}    
              }
               system.debug('TotalForcast: ' +TotalForcastparc);
           }
            Opp = [Select id,Parcels_Forecasted__c,Amount, Total_Forecast_Domestic_Parcels__c, Total_Forecast_Domestic__c,Total_Forecast_International_Parcels__c,Total_Foreast_International__c from Opportunity  where id =:  o.Id];
            system.debug('opp: ' +Opp);
    if (Opp.size()>0)
    {
          for (Opportunity Opport: Opp) 
          {
              system.debug('ok:' );
            Opport.Amount    = TotalForcastcurrency;
            Opport.Parcels_Forecasted__c    = TotalForcastparc;
            Opport.Total_Forecast_Domestic_Parcels__c = TotalForcastDomPorc;
            Opport.Total_Forecast_Domestic__c  = TotalForcastDomcurrency;
            Opport.Total_Forecast_International_Parcels__c =TotalForcastIntparc;  
            Opport.Total_Foreast_International__c  =  TotalForcastIntcurrency;
            Opport.Nb_BUs_participating__c =   Nb_BUs_participating;
            upsert (Opport);
             }  
    }
         }
        
    }
    
    
}


I created test class

@isTest
public class Opp_CalculTotalForcast_Test {
    
@isTest
static void Opp_CalculTotalForcast_Test()
    {
        //Profil admin pour lancer le trigger
        
        Profile p = [select id from profile where Name='System Administrator' limit 1];   
        
        User u = new User(alias = 'test3', email='testemail@gmail.com', emailencodingkey='ISO-8859-1', lastname='Testing', languagelocalekey='fr',   
        localesidkey='fr_FR_EURO', profileid = p.Id, timezonesidkey='Europe/Paris', username='test1834'+String.valueOf(date.today())+'@csc.com');   
        insert u;
          system.runAs(u){
        //debut test    
     system.test.startTest();
     DateTime myDateTime = DateTime.newInstance(2014, 12, 16, 12, 6, 13);
     Date myDate = myDateTime.date();
     Id RecordTypeIdBUtypeAccount = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Account').getRecordTypeId();
        //Create new account  
     Account acc = new Account (Name = 'testdeaicha23', recordTypeId = RecordTypeIdBUtypeAccount);
     insert acc;
      Account acc1 = new Account (Name = 'testdeaicha2', recordTypeId = RecordTypeIdBUtypeAccount);
     insert acc1;
    
      //create new opportunity      
     Opportunity opp = new Opportunity (AccountId = acc.id, Name = 'opptestaicha', StageName = '0. Pre-Alert', CloseDate = myDate);
     opp.Date_Received__c=Date.today();
     opp.Deadline_to_respond__c=Date.today();
     insert opp;    
              // create new BU Response attached to Opportunity
     Tender_BU_Response__c  BURespons = new Tender_BU_Response__c (Tier__c = acc.id,Tender__c=opp.id,  Name= 'BUtestaicha1', Domestic_New_Business_Parcels__c= 100, Domestic_Revenues_Forecast__c= 200, International_Parcels_Forecast__c    = 400, International_Revenues_Forecast__c= 350 );
     insert BURespons;
       update opp;

        system.test.stopTest();
          }
    }
}

But covrage is 64%, can someone help me
bhanu_prakashbhanu_prakash
Hi Dev87,

Create new method which statisfes condtions like   if (allBUList.size()>0) ,  if (Opp.size()>0) .
Please share code coverage lines which highlighted lines which are not covered helps us to do best 

Mark as resloved if it helps :) :)
Thanks, 
Bhanu Prakash
visit ForceLearn.com ​ (https://www.forcelearn.com)
 
Dev87Dev87
Hello, 
Thanks for replaying, I have a probleme whith my Tender_BU_Response__c   list.


User-added image