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 

Test Class - Coverage

Hello, 
I created update trigger to update opportunity and I have a probleme with covrage.
This is my 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<Tender_BU_Response__c> BUREPONSE = new list<Tender_BU_Response__c>();
               
        list<Opportunity> Opp = new list<Opportunity>();
       
         for(Opportunity o:oppList ){
          Id  oppId= o.id;
             system.debug('oppId--' +oppId);
          string soqlQuery = 'Select id,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 =:oppId';
          BUREPONSE = database.query(soqlQuery);   
       system.debug('BUREPONSE.size()--' +BUREPONSE.size());      
      if (BUREPONSE.size()!= NULL)
           {  
               system.debug('test1---');
            for (i=0; i < BUREPONSE.size(); i++)
             { 
               {Nb_BUs_participating = Nb_BUs_participating+i;} 
               if (BUREPONSE[i].Total_Parcels_Forecast__c != NULL)
               { TotalForcastparc = TotalForcastparc +BUREPONSE[i].Total_Parcels_Forecast__c;}                             
               if (BUREPONSE[i].Total_Revenues_Forecast__c != NULL)    
               {TotalForcastcurrency = TotalForcastcurrency +  BUREPONSE[i].Total_Revenues_Forecast__c;}    
               if (BUREPONSE[i].Domestic_Parcels_Forecast__c != NULL)
               {TotalForcastDomPorc = TotalForcastDomPorc +  BUREPONSE[i].Domestic_Parcels_Forecast__c;} 
               if (BUREPONSE[i].Domestic_Revenues_Forecast__c != NULL)
               {TotalForcastDomcurrency = TotalForcastDomcurrency + BUREPONSE[i].Domestic_Revenues_Forecast__c;}
               if (BUREPONSE[i].International_Parcels_Forecast__c != NULL)
               { TotalForcastIntparc = TotalForcastIntparc + BUREPONSE[i].International_Parcels_Forecast__c;}
               if (BUREPONSE[i].International_Revenues_Forecast__c != NULL)
               {TotalForcastIntcurrency = TotalForcastIntcurrency + BUREPONSE[i].International_Revenues_Forecast__c;}
                    
              }
           }
            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];
    if (Opp.size()>0)
    {
          for (Opportunity Opport: Opp) 
          {
            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);
             }  
    }
       }
        
    }
    
    
}
this is my trigger 
 
trigger OppAfterUpdate on Opportunity (after update) {

Set<String> accIdSet = new Set<String>();
List <Opportunity> oppList = new List<Opportunity>();
if(checkRecursive.isFirstTime)
 {
checkRecursive.isFirstTime = false; 
  for(Opportunity Opp:Trigger.new){
	     {
	      oppList.add(Opp);
	     }
		}
    if(!oppList.isEmpty()){
            Opp_CalculTotalForcast.TotalForcast(oppList);
        }}

}

and this is my test class: 
@isTest(SeeAllData=true)
public class Opp_CalculTotalForcasttest {
@isTest
static void Opp_CalculTotalForcast_Test()
    {
          system.test.startTest();
          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){
          id  recordtypeid;
        List <recordtype> RecordtypeOpp= [Select id  from recordtype  where id =: Label.GATERFI  limit 1];
        if (RecordtypeOpp.size() > 0)
        {
            recordtypeid = RecordtypeOpp[0].id;
        } 
     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;
              system.debug('accid--' +acc.id);
      //create new opportunity      
     Opportunity opp = new Opportunity (AccountId = acc.id, Name = 'opptestaicha', StageName = '0. Pre-Alert', CloseDate = myDate,Date_Received__c=Date.today(),Deadline_to_respond__c=Date.today());
    insert opp;   
   system.debug('oppid--' +opp.id);
   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.debug('BUResponsinset ' +BURespons.id);   
      opp.Comments__c= '--'    ;     
   //  update opp;
   system.debug('oppid2 ' +opp.id); 
          }
    } 
}


My covarege is 68%.

User-added image
 
Amit Chaudhary 8Amit Chaudhary 8

Please try below code
@isTest(SeeAllData=true)
public class Opp_CalculTotalForcasttest {
@isTest
static void Opp_CalculTotalForcast_Test()
    {
          system.test.startTest();
          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){
          id  recordtypeid;
        List <recordtype> RecordtypeOpp= [Select id  from recordtype  where id =: Label.GATERFI  limit 1];
        if (RecordtypeOpp.size() > 0)
        {
            recordtypeid = RecordtypeOpp[0].id;
        } 
     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;
              system.debug('accid--' +acc.id);
      //create new opportunity      
     Opportunity opp = new Opportunity (AccountId = acc.id, Name = 'opptestaicha', StageName = '0. Pre-Alert', CloseDate = myDate,Date_Received__c=Date.today(),Deadline_to_respond__c=Date.today());
    insert opp;   
   system.debug('oppid--' +opp.id);
   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;

			Test.StartTest();
				checkRecursive.isFirstTime = true;
				update opp;
				system.debug('BUResponsinset ' +BURespons.id);   
				opp.Comments__c= '--'    ;     
				system.debug('oppid2 ' +opp.id); 
				
			Test.StopTest();
          }
    } 
}

Let us know if the if this will help you