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
Naveen saini 10Naveen saini 10 

Getting low code coverage

@isTest(seeAllData = true)
public class OpportunityAddOnTest 
{
    public static TestMethod void MyTest()
    {
        List<Opportunity> OpportunityList = new List<Opportunity>();
        
        Opportunity Opp1 = new Opportunity();
        opp1.Name = 'opportnity_one';
        opp1.CloseDate = date.today()-15;
        opp1.StageName= 'closed won';
        insert opp1;
 
        Opportunity Opp = new Opportunity();
        opp.Name = 'opportnity_one';
        opp.CloseDate = date.today()-15;
        opp.StageName= 'closed won';
        opp.Opportunity__c = opp1.id;
        insert opp;
      
        OpportunityList.add(opp);
        
    }

}
this is my test class

 
trigger OpportunityAddOn on Opportunity (after insert,after update)
{
    if(!test1.IsOpptyUpdate)
    {
        test1.IsOpptyUpdate = true;
    
        List<Opportunity> updateList = new List<Opportunity>();
     
            set<id> oppId=new set<id>();
         
            for(Opportunity opp:trigger.new)
            {
                if(opp.Opportunity__c !=null)
                {
                    oppId.add(opp.Opportunity__c);
                    System.debug('parent id is-->' +opp.Opportunity__c);
                }
                
            }
         
     for(Opportunity partopp : [select id,Add_On_ARR__c,(select id,Opportunity_ARR__c from opportunities__r) from Opportunity where id in:oppId]){
      Double countval = 0;
      if(partopp.opportunities__r.Size() > 0)
      { 
      
       for(Opportunity child_op : partopp.opportunities__r)
       {
           
           if(child_op.Opportunity_ARR__c != NULL ) {
         
            countval = child_op.Opportunity_ARR__c + countval ;
           
           }
          
       } 
         if(countval > 0)
          {
            partopp.Add_On_ARR__c =countval ;
            updateList.add(partopp);
            
          }
       }
     
     }
     if(!updateList.isEmpty())
     {
         update updateList;
     }
    }
}

this is my trigger..
 
Amit Chaudhary 8Amit Chaudhary 8
Please try below test class and let us know the result
@isTest
public class OpportunityAddOnTest 
{
    public static TestMethod void MyTest()
    {
		Account acc = new Account();
		acc.Name='Test Acc';
		insert acc;
	
        List<Opportunity> OpportunityList = new List<Opportunity>();
        
        Opportunity Opp1 = new Opportunity();
		opp1.accountid =acc.id;
		opp1.Opportunity_ARR__c = 1;
        opp1.Name = 'opportnity_one';
        opp1.CloseDate = date.today()-15;
        opp1.StageName= 'closed won';
        insert opp1;
			
		
        Opportunity Opp = new Opportunity();
		opp.accountid =acc.id;
        opp.Name = 'opportnity_one';
        opp.CloseDate = date.today()-15;
        opp.StageName= 'closed won';
        opp.Opportunity__c = opp1.id;
        insert opp;
      
        OpportunityList.add(opp);
        
		
    }

}

 
Dhanya NDhanya N
Hi Naveen,

Try with inserting records for opportunities__r.

Please refer below code.
 
@isTest
private class OpportunityAddOnTest 
{
    @isTest private static void MyTest()
    {
        List<Opportunity> OpportunityList = new List<Opportunity>();
        
        Opportunity Opp1 = new Opportunity();
        opp1.Name = 'opportnity_one';
        opp1.CloseDate = date.today()-15;
        opp1.StageName= 'closed won';
        insert opp1;
 
        Opportunity Opp = new Opportunity();
        opp.Name = 'opportnity_one';
        opp.CloseDate = date.today()-15;
        opp.StageName= 'closed won';
        opp.Opportunity__c = opp1.id;
        insert opp;
      
        OpportunityList.add(opp);
		
	opportunities__c obj = new opportunities__c();
	obj.Opportunity__c = opp.Id;
	obj.Opportunity_ARR__c = 23;  
        insert obj;
    }
}
Thanks,
Dhanya
Naveen saini 10Naveen saini 10
showing error amit and dhanya...
Amit Chaudhary 8Amit Chaudhary 8
Hi Naveen,

Please let us know what error you are getting. Please share screen shot of error
Naveen saini 10Naveen saini 10
Invalid type: opportunities__c @dhanya


System.DmlException: Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Jhall Bhugga: [Mobile__c] @ Amit
Amit Chaudhary 8Amit Chaudhary 8
Can you please check on which object you have below validation and please share validation rule screen shot.

FIELD_CUSTOM_VALIDATION_EXCEPTION, Jhall Bhugga: [Mobile__c]
Naveen saini 10Naveen saini 10
No validation rule on opportnity
Amit Chaudhary 8Amit Chaudhary 8
Please try below test class.
@isTest
public class OpportunityAddOnTest 
{
    public static TestMethod void MyTest()
    {
		Account acc = new Account();
		acc.Name='Test Acc';
		acc.phone = 1234567890;
		insert acc;
	
        List<Opportunity> OpportunityList = new List<Opportunity>();
        
        Opportunity Opp1 = new Opportunity();
		opp1.accountid =acc.id;
		opp1.Opportunity_ARR__c = 1;
        opp1.Name = 'opportnity_one';
        opp1.CloseDate = date.today()-15;
        opp1.StageName= 'closed won';
        insert opp1;
			
		
        Opportunity Opp = new Opportunity();
		opp.accountid =acc.id;
        opp.Name = 'opportnity_one';
        opp.CloseDate = date.today()-15;
        opp.StageName= 'closed won';
        opp.Opportunity__c = opp1.id;
        insert opp;
		
		
		update opp;
    }

}

 
Naveen saini 10Naveen saini 10
still getting 45% coverage and y do we need account object
Dhanya NDhanya N
Which lines are not covering?
Dhanya NDhanya N
In line no. 21, you have used (select id,Opportunity_ARR__c fromopportunities__r) query.
So itseems that there is some custom object related to Opportunity. So try inserting record for that custom object.
Amit Chaudhary 8Amit Chaudhary 8
Please try below code. Account object we created to set Account lookup on opportunity
@isTest
public class OpportunityAddOnTest 
{
    public static TestMethod void MyTest()
    {
		Account acc = new Account();
		acc.Name='Test Acc';
		acc.phone = 1234567890;
		insert acc;
	
        List<Opportunity> OpportunityList = new List<Opportunity>();
        
        Opportunity Opp1 = new Opportunity();
		opp1.accountid =acc.id;
		opp1.Opportunity_ARR__c = 1;
        opp1.Name = 'opportnity_one';
        opp1.CloseDate = date.today()-15;
        opp1.StageName= 'closed won';
        insert opp1;
			
		Test.StartTest();
		
			Opportunity Opp = new Opportunity();
			opp.accountid =acc.id;
			opp.Name = 'opportnity_one';
			opp.CloseDate = date.today()-15;
			opp.StageName= 'closed won';
			opp.Opportunity__c = opp1.id;
			insert opp;
			
			update opp;
		Test.StopTest();	
			
    }

}
After executing above code can you please let us know which line are covering ??
 
Naveen saini 10Naveen saini 10
same as earlier.no change
Amit Chaudhary 8Amit Chaudhary 8
It look like some issue is coming because of below class

test1.IsOpptyUpdate


Please try below code

@isTest
public class OpportunityAddOnTest
{
    public static TestMethod void MyTest()
    {
        Account acc = new Account();
        acc.Name='Test Acc';
        acc.phone = 1234567890;
        insert acc;
    
        List<Opportunity> OpportunityList = new List<Opportunity>();
        
        Opportunity Opp1 = new Opportunity();
        opp1.accountid =acc.id;
        opp1.Opportunity_ARR__c = 1;
        opp1.Name = 'opportnity_one';
        opp1.CloseDate = date.today()-15;
        opp1.StageName= 'closed won';
        insert opp1;
            
        Test.StartTest();
        
            Opportunity Opp = new Opportunity();
            opp.accountid =acc.id;
            opp.Name = 'opportnity_one';
            opp.CloseDate = date.today()-15;
            opp.StageName= 'closed won';
            opp.Opportunity__c = opp1.id;
            insert opp;
            
            test1.IsOpptyUpdate = false;
 
           
            update opp;
        Test.StopTest();    
            
    }

}
Amit Chaudhary 8Amit Chaudhary 8
Ohh Grt Finally your code coverage is more then 75%.

Please try to set Opportunity_ARR__c in both opportunity like below.
Try below code .
@isTest
public class OpportunityAddOnTest
{
    public static TestMethod void MyTest()
    {
        Account acc = new Account();
        acc.Name='Test Acc';
        acc.phone = 1234567890;
        insert acc;
    
        List<Opportunity> OpportunityList = new List<Opportunity>();
        
        Opportunity Opp1 = new Opportunity();
        opp1.accountid =acc.id;
        opp1.Opportunity_ARR__c = 1;
        opp1.Name = 'opportnity_one';
        opp1.CloseDate = date.today()-15;
        opp1.StageName= 'closed won';
        insert opp1;
            
        Test.StartTest();
        
            Opportunity Opp = new Opportunity();
            opp.accountid =acc.id;
            opp.Name = 'opportnity_one';
			opp.Opportunity_ARR__c = 1;
            opp.CloseDate = date.today()-15;
            opp.StageName= 'closed won';
            opp.Opportunity__c = opp1.id;
            insert opp;
            
            test1.IsOpptyUpdate = false;
            
            update opp;
        Test.StopTest();    
            
    }

}


I hope 80% is good to go