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
mahe_developermahe_developer 

Code coverage across all Apex Classes and Triggers is 73%, at least 75% test coverage is required

Hi,

 

         All My test classes average  code coverage is 84%, But i am always getting Average test coverage across all Apex Classes and Triggers is 73%, at least 75% test coverage is required error while deploying into Production.

What Might be the Problem with this.

 

 

Thanks

 

Mahesh....

Best Answer chosen by Admin (Salesforce Developers) 
Kevin SwiggumKevin Swiggum

Usually this has to do with the data in test vs. production. It could be that there are parts of code that don't run because the data present in production doesn't fit the test conditions necessary for your unit tests. No silver bullet to solve this other than to walk through your unit tests and look for data dependencies (queries against existing data, specific records, user account runAs user stuff)

 

You should also look for any differences between test and production in terms of validation rules, workflow rules, etc. Sometimes for large deployments, I'll deploy my object changes (custom fields, etc) and workflow first...and then deploy code.

 

hope that helps

--Kevin

All Answers

Kevin SwiggumKevin Swiggum

Usually this has to do with the data in test vs. production. It could be that there are parts of code that don't run because the data present in production doesn't fit the test conditions necessary for your unit tests. No silver bullet to solve this other than to walk through your unit tests and look for data dependencies (queries against existing data, specific records, user account runAs user stuff)

 

You should also look for any differences between test and production in terms of validation rules, workflow rules, etc. Sometimes for large deployments, I'll deploy my object changes (custom fields, etc) and workflow first...and then deploy code.

 

hope that helps

--Kevin

This was selected as the best answer
Jon Mountjoy_Jon Mountjoy_

Excellent insight Kevin - I was scratching my head about this one. 

goabhigogoabhigo

I am sure you are querying some records that exists in sandbox but not in production. The best way to solve this error is to insert some dummy records in testMethod then call the class.

 

Suppose if your code is:

Lead l = [select id,name,lead_number__c from Lead where name='Sample' limit 1];
CustomObj__c obj = new CustomObj__c();
obj.LeadName__c = l.Name;                                                                  ApexPages.StandardController ap = new ApexPages.StandardController(obj);                  MyController mc = new MyContrller(ap);                                                    mc.method1();                                                                                   mc.method2();

 Modify this code to:

 

Lead l = new Lead(lastName='testTrigger',Status='Qualified',Company='Sample';
insert l;
CustomObj__c obj = new CustomObj__c();
obj.LeadName__c = l.Name;
ApexPages.StandardController ap = new ApexPages.StandardController(obj);
MyController mc = new MyContrller(ap);
mc.method1();
mc.method2();

 

I had come across the same in the past. Hope this helps.

mahe_developermahe_developer

Perfect Kevin....... Thanks for Your.........

 

mahi

Shiva Ramesh @ xcdhrShiva Ramesh @ xcdhr

Hi kevin_swiggum,

 

I am also facing this same issue. Can you explain me How the test class will depends on production data? 

Saravanan @CreationSaravanan @Creation

Hi Kevin

 

Thanks for this post.

 

can you explain me more briefly about this.because I also facing this problem i couldn't able to found out 

 

In my test class I am not using organization data I created the data in my test class itself.then how it will get differ from 

test to production 

 

 

Thanks in advance