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
EIE50EIE50 

Test Class working fine in prod but not in sandbox anymore

Hi,

 

I deployed an app into prod before couple of months and all the test classes were fine with no errors with 75% coverage. Today i tried to run a test in my sandbox and a lot of errors were thrown with test % coming down to 48. So, i deployed test classes from prod to the sandbox and tried again, the result is same. Why is this happening though when i run the same test class in prod it has no errors with 75% where as in sandbox a lot of errors are thrown with 48% . Does any body know what is causing the problem or why such problem persists. FYI, my sandbox is not yet been upgraded to summer release and prod too not yet upgraded to summer release. 

 

Thanks.

Best Answer chosen by Admin (Salesforce Developers) 
BrianWKBrianWK

I too have had this issue. Here's a list of things I go through each time something like this happens 99.99% of time it's because I missed something in my test class.

 

  1. Test Objects -- Are you creating ALL your test objects in the test methods? Are you querying anything that is a needed record and can only be found on Production? Think of Users, Accounts, contacts, custom objects etc
  2. Did anything on your Sandbox change? Add a validation rule? InActivate a user? Change object field requirements?
  3. Did you add any code that might impact your trigger/class you're testing?

Normally those 3 items plus the error messages from the test will help me figure out what's going on. Typically, if it's something from Production that is just now failing those three items are it. Other issues I've run into tend to be issues with Sandbox going to Production:

 

  1. Querying the right objects -- querying just the Test objects you created? Querying all the correct fields you need?
  2. Test method actually calling the Trigger/Class you're testing? I know this sounds stupid, but I actually wrote a few hundred lines of test code just to forget to actually call the class I was testing. 
  3. Re-Querying Objects before you do a system.assert. I find this important when your testing classes that don't return anything - simply updates the record. If you don't re-query sometimes the Object variable in your test class just won't reflect what the object has for values after it's been run through that class
  4. Mis-Type your system asserts? I know this sounds painfully obvious too. I've had times where I thought I was pulling my hair out looking at debug code and screaming at the screen "It says correct right there!" only to find I "Fat Fingered" my system.assert statement

 

I hope  this helps. If you need more guidance, I recommend pasting your test code here as well as your error messages.

 

 

All Answers

BrianWKBrianWK

I too have had this issue. Here's a list of things I go through each time something like this happens 99.99% of time it's because I missed something in my test class.

 

  1. Test Objects -- Are you creating ALL your test objects in the test methods? Are you querying anything that is a needed record and can only be found on Production? Think of Users, Accounts, contacts, custom objects etc
  2. Did anything on your Sandbox change? Add a validation rule? InActivate a user? Change object field requirements?
  3. Did you add any code that might impact your trigger/class you're testing?

Normally those 3 items plus the error messages from the test will help me figure out what's going on. Typically, if it's something from Production that is just now failing those three items are it. Other issues I've run into tend to be issues with Sandbox going to Production:

 

  1. Querying the right objects -- querying just the Test objects you created? Querying all the correct fields you need?
  2. Test method actually calling the Trigger/Class you're testing? I know this sounds stupid, but I actually wrote a few hundred lines of test code just to forget to actually call the class I was testing. 
  3. Re-Querying Objects before you do a system.assert. I find this important when your testing classes that don't return anything - simply updates the record. If you don't re-query sometimes the Object variable in your test class just won't reflect what the object has for values after it's been run through that class
  4. Mis-Type your system asserts? I know this sounds painfully obvious too. I've had times where I thought I was pulling my hair out looking at debug code and screaming at the screen "It says correct right there!" only to find I "Fat Fingered" my system.assert statement

 

I hope  this helps. If you need more guidance, I recommend pasting your test code here as well as your error messages.

 

 

This was selected as the best answer
ScoobieScoobie

In addition to the advice above, I'd make a special check of all the if conditions as if an environmental change has been made you may be skipping 20 lines of code because an if clause which previously validated to true now validates to false.

EIE50EIE50

Hi,

 

Thanks for the reply, i was able to fix the failures. I missed out an IF condition and it was causing some failures and a custom field's lenght was creating some failures. After i included the missing IF condition and increased the lenght of my Custom Field text type, everything works fine, though the test % has come down a lot. Got to work on Test Classes again, which i hate doing it.

 

Thanks.