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
Neal LightfeldtNeal Lightfeldt 

Apex Test Failures Code Coverage 40%

I am getting these Apex Test Failures when validating an inbound change set in production. None of these have to do with the app I am moving from Sandbox. I have zero knowledge of Apex and I just want to know what I have to do to resolve these errors. Can anyone help?User-added image
Christan G 4Christan G 4
In order to migrate code to Production, the overall test code percentage needs to be at least 75%. In the screenshot, Salesforce is informing you that your overall code percentage is 40% and thus, can not be migrated. It is showing you all the issues that it found when running those test classes. If none of these have anything to do with the app that you are moving from Sandbox, then I would suggest removing all Apex code from your change set. However, if some of the Apex code you have is relevant to perform some of the functionality within the app, then this will be tricky. I've never migrated via change sets but I believe code used within an app must be deployed together. If that is the case, then you will have to resolve these issues. I suggest contact the developer who created these Apex classes and inquire if they can fix these test classes. If not, I can help resolve some. At a first glance of the error messages, it seems there is a required field that is not mentioned within the code. Thus, causing these errors. 
Neal LightfeldtNeal Lightfeldt
Thanks Christian. The Apex errors have nothing to do with what I am trying to migrate to production. Right now I am going into Sandbox, commenting out the offending classes, then moving to production to see if that will eliminate the errors. 
Andrew GAndrew G
Hi Neal

When Salesforce validates a change set , by default, it runs all custom Test code.  Whilst the change you are trying to validate may have nothing to do with that particular code, all test classes in production will run and they would indicate an issue with your code.  

In your Sandbox, assuming it is in sync with your production environment, run all the test code and see what errors occur.  This would indicate whether there is a need to update the code that is currently erroring.

Alternate, and i'm loathe to point this out, but you can deploy a changeset and have it run only selected Test Classes.  
User-added image

Depending on the App you are trying to deploy/validate, if you know the test classes related purely to the Apex code you are deploying, you can select those test classes.

I am very, very wary of using this method to deploy, as I believe, it leads to poor overall system test coverage, remembering that test coverage is more than just lines covered, it should be testing that the expected outcome of your custom code does exactly as expected. 

The errors you are seeing could be due to the changes that you are deploying without realising their impact on other parts of your SF environment. 

Regards
Andrew