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
Soundar Rajan PonpandiSoundar Rajan Ponpandi 

Test Class Not Coverage

Hi Friends,

I am trying to cover a test class for below Trigger. But it's not cover as well , yeah it's just 42% only . already i have inserted Account and Opportunity also. But Still it's Not Covering. Kindly give me a valid advise ! Why it's happening .

************************ Trigget *******************
trigger OpportunityRollUp on Opportunity  ( after insert, after update,after delete) {
     
       Set<ID> accSet = New Set<ID>();
       List<Account> accList  = New List<Account>();
      
      If(Trigger.IsDelete){
        
        for(Opportunity opp1 : Trigger.old){  /* Not Covered */
            accSet.add(opp1.AccountId);   /* Not Covered */
        }
      }
      
      If(Trigger.IsUpdate){
        for(Opportunity opp : Trigger.new){
        
            if(opp.Amount != Trigger.oldMap.get(opp.id).amount && opp.AccountId != Null ){
            
                accSet.add(opp.AccountId);  /* Not Covered */
            }        
        }          
      }
      

      if(!accSet.isEmpty()){
       /* Below Aggregate Result is Not Covered */
        for(AggregateResult age : [Select AccountId,sum(Amount)tot, avg(Amount)opAvg, Min(Amount)opMin,  Max(Amount)opMax  from opportunity     
                    Where AccountId IN :accSet GROUP BY AccountId]){
        
                  accList.add(New Account(Id = (ID)age.get('AccountId'), 
                                         opportunity_Total_Amount__c = (Decimal)age.get('tot'),
                                         Opportunity_Average__c = (Decimal)age.get('opAvg'),
                                         Opportunity_Max__c = (Decimal)age.get('opMax'),
                                         Opportunity_Min__c = (Decimal)age.get('opMin')));
        }
      }  
      
      if(accList.size() > 0){
          update accList;
      } 
      

}

*****************TEST CLASS ********************
@IsTest
Public Class OpportunityRollUp_Test{

public static testMethod void oppRollup(){


//Insert Account
Account acc = New Account();
acc.name = 'Test_Account';
acc.Account_Code__c = 'AP001';
acc.Opportunities_Total_Amount__c = '1000';
acc.Total_Opportunities__c = 5;
acc.Opportunity_Average__c = 1000;
acc.Opportunity_Min__c = 500;
acc.Opportunity_Max__c = 1000;
insert acc;
system.debug('Insert Account Details' + acc);

//insert CurrencyType
CurrencyType__c ctype = New CurrencyType__c();
ctype.name = 'Test_Type';
ctype.Conversion_Rate__c = 50;
ctype.ISO_Code__c = 'INR';
insert ctype;
system.debug('Insert Currency Type details : ' + ctype);

CurrencyType__c ctype1 = New CurrencyType__c();
ctype1.name = 'Test_Type';
ctype1.Conversion_Rate__c = 50;
ctype1.ISO_Code__c = 'AUD';
insert ctype1;

//Insert Oportunity
Opportunity Opp  = New Opportunity();
opp.name = 'Test_Opportunity';
opp.closeDate = System.today();
opp.stageName = 'Closed Won';
opp.Amount = 1000;
opp.Currency__c = 'INR';
opp.USD_Amount1__c = 1000;
opp.AccountId = acc.id;
insert opp;
system.debug('Insert Opp details : ' + opp);


Opportunity Opp1  = New Opportunity();
Opp1.name = 'Test_Opportunity';
Opp1.closeDate = System.today();
Opp1.stageName = 'Closed Won';
Opp1.Amount = 1000;
Opp1.Currency__c = 'INR';
Opp1.USD_Amount1__c = 1000;
Opp1.AccountId = acc.id;
insert opp1;
Opp1.Currency__c = 'AUD';
update opp1;
} 

}

Thanks In Advance...

Regards,
Soundar.
 
Best Answer chosen by Soundar Rajan Ponpandi
Amit Chaudhary 8Amit Chaudhary 8
NOTE:- In your test class you need to update Amount and need to delete opp to get full code coverage

Update your test class like below
@IsTest
Public Class OpportunityRollUp_Test{

	public static testMethod void oppRollup()
	{
		//Insert Account
		Account acc = New Account();
		acc.name = 'Test_Account';
		acc.Account_Code__c = 'AP001';
		acc.Opportunities_Total_Amount__c = '1000';
		acc.Total_Opportunities__c = 5;
		acc.Opportunity_Average__c = 1000;
		acc.Opportunity_Min__c = 500;
		acc.Opportunity_Max__c = 1000;
		insert acc;
		system.debug('Insert Account Details' + acc);

		//insert CurrencyType
		CurrencyType__c ctype = New CurrencyType__c();
		ctype.name = 'Test_Type';
		ctype.Conversion_Rate__c = 50;
		ctype.ISO_Code__c = 'INR';
		insert ctype;
		system.debug('Insert Currency Type details : ' + ctype);

		CurrencyType__c ctype1 = New CurrencyType__c();
		ctype1.name = 'Test_Type';
		ctype1.Conversion_Rate__c = 50;
		ctype1.ISO_Code__c = 'AUD';
		insert ctype1;

		//Insert Oportunity
		Opportunity Opp  = New Opportunity();
		opp.name = 'Test_Opportunity';
		opp.closeDate = System.today();
		opp.stageName = 'Closed Won';
		opp.Amount = 1000;
		opp.Currency__c = 'INR';
		opp.USD_Amount1__c = 1000;
		opp.AccountId = acc.id;
		insert opp;
		system.debug('Insert Opp details : ' + opp);
		
		opp.Amount = 2000;
		update opp;
		Delete opp;
	} 

}

Let us know if this will help you
 

All Answers

vikas rathi91vikas rathi91
Opportunity Opp1  = New Opportunity();
Opp1.name = 'Test_Opportunity';
Opp1.closeDate = System.today();
Opp1.stageName = 'Closed Won';
Opp1.Amount = 1000;
Opp1.Currency__c = 'INR';
Opp1.USD_Amount1__c = 1000;
Opp1.AccountId = acc.id;
insert opp1;
Opp1.Currency__c = 'AUD';
update opp1;
delete opp1;

add the code block in your code


Hope Your trigger code cover above 75%
 

mark it best answer if it resove your problem

thanks
vikas

Varun SinghVarun Singh
HI @ Sounder Raj,

I modified your test class ,you will  get 100% coverage just copy paste my code.
@IsTest
Public Class OpportunityRollUp_Test{

public static testMethod void oppRollup(){


//Insert Account
    Account acc = New Account();
    acc.name = 'Test_Account';
    acc.Opportunities_Total_Amount__c = '1000';
    acc.Opportunity_Average__c = 1000;
    acc.Opportunity_Min__c = 500;
    acc.Opportunity_Max__c = 1000;
    insert acc;
    system.debug('Insert Account Details' + acc);



    set<id> id=new Set<id>();
    
//Insert Oportunity
    Opportunity Opp  = New Opportunity();
    opp.name = 'Test_Opportunity';
    opp.closeDate = System.today();
    opp.stageName = 'Closed Won';
    opp.Amount = 1000;

    opp.AccountId = acc.id;
    insert opp;
    id.add(opp.accountid);
    system.debug('Insert Opp details : ' + opp);
    



    List<Account> lstupdate=new List<Account>();
    List<account> acclst=[select id,Opportunities_Total_Amount__c ,Opportunity_Average__c ,Opportunity_Min__c ,Opportunity_Max__c from Account where id in:id ];
    for(Account a:acclst){
    a.name = 'Test_Account';
    a.Opportunities_Total_Amount__c = '1000';
    a.Opportunity_Average__c = 1000;
    a.Opportunity_Min__c = 500;
    a.Opportunity_Max__c = 1000;
    lstupdate.add(a);
    }
    update lstupdate;
    
    
    opp.Amount = 2000;
    
    opp.name='testvarun';
    update opp;
    delete opp;
} 

}


If its work for you make my answer as best answer.
Amit Chaudhary 8Amit Chaudhary 8
NOTE:- In your test class you need to update Amount and need to delete opp to get full code coverage

Update your test class like below
@IsTest
Public Class OpportunityRollUp_Test{

	public static testMethod void oppRollup()
	{
		//Insert Account
		Account acc = New Account();
		acc.name = 'Test_Account';
		acc.Account_Code__c = 'AP001';
		acc.Opportunities_Total_Amount__c = '1000';
		acc.Total_Opportunities__c = 5;
		acc.Opportunity_Average__c = 1000;
		acc.Opportunity_Min__c = 500;
		acc.Opportunity_Max__c = 1000;
		insert acc;
		system.debug('Insert Account Details' + acc);

		//insert CurrencyType
		CurrencyType__c ctype = New CurrencyType__c();
		ctype.name = 'Test_Type';
		ctype.Conversion_Rate__c = 50;
		ctype.ISO_Code__c = 'INR';
		insert ctype;
		system.debug('Insert Currency Type details : ' + ctype);

		CurrencyType__c ctype1 = New CurrencyType__c();
		ctype1.name = 'Test_Type';
		ctype1.Conversion_Rate__c = 50;
		ctype1.ISO_Code__c = 'AUD';
		insert ctype1;

		//Insert Oportunity
		Opportunity Opp  = New Opportunity();
		opp.name = 'Test_Opportunity';
		opp.closeDate = System.today();
		opp.stageName = 'Closed Won';
		opp.Amount = 1000;
		opp.Currency__c = 'INR';
		opp.USD_Amount1__c = 1000;
		opp.AccountId = acc.id;
		insert opp;
		system.debug('Insert Opp details : ' + opp);
		
		opp.Amount = 2000;
		update opp;
		Delete opp;
	} 

}

Let us know if this will help you
 
This was selected as the best answer
Soundar Rajan PonpandiSoundar Rajan Ponpandi
Hi Amit Chaudhary,

Your Code is Covering 100 % (What i am expecting). Really Thanks , and I understood as well where i did mistakes.

Thanks !!
Soundar Rajan PonpandiSoundar Rajan Ponpandi
Hi VikasRathi & Varun Singh,

Thanks For Your Quick Response. :)


Thanks !!
Soundar