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
SivaGSivaG 

Test Methods failing in Prodcopy sandbox

Hi,

I have 3 Apex classes in Dev Sandbox and details are given below.

Code coverage after testing.
Case - 79% - 51/64
Lead - 66% - 101/152
Opp - 78% - 221/280

Code deployed to Prodcopy Sandbox along with test class and ran the test class.

Code coverage after running the test class. For some reason code coverage is showing some old data.
Case - 72% - 42/58
Lead - 84% - 131/155
Opp - 85% - 193/226

Code matches between Dev & Prodcopy Sandbox. I am not sure what's wrong with Prodcopy Sandbox. When I ran the test class on Friday evening 1 test method failed out of 21.(Assertion failure) and it didn't fail in Dev Sandbox.

Now whenever I ran the test class since Yesterday, 5 test methods are failing with 'Attempt to de-reference a null object' in Opp class at same line. Still all 21 test methods are passing in Dev Sandbox. 

Please help me.

Thanks
Kumar
Best Answer chosen by SivaG
James LoghryJames Loghry

Do any of your tests depend on existing data?  Are they older than api version 18, or use the (@SeeAllData=true)?
 

If so, it's likely data changed in your Prodcopy class and broke your tests.

The best practice to avoid this is to update your unit tests, removing any dependency on existing data on your org.  You do this by removing the SeeAllData annotation and creating your own mock records.

For more info, read this document: http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_testing_data_access.htm

All Answers

James LoghryJames Loghry

Do any of your tests depend on existing data?  Are they older than api version 18, or use the (@SeeAllData=true)?
 

If so, it's likely data changed in your Prodcopy class and broke your tests.

The best practice to avoid this is to update your unit tests, removing any dependency on existing data on your org.  You do this by removing the SeeAllData annotation and creating your own mock records.

For more info, read this document: http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_testing_data_access.htm

This was selected as the best answer
SivaGSivaG
James,

I agree with you to remove SeeAllData=true annotation from my test class and I tried doing it. 

My scenario is little different as explained below.

Driver for my Batch class is Custom object, based on some criteria my batch class is calling 3 different handler classes Lead/Case/Opportunity to handle the inserts/updates to Lead/Account/Opp/Case objects.
There are minimum 10 triggers(like before insert/after insert/before update/after update) for each of these objects. In each of these triggers they access some existing data using SOQL queries. If any of SOQL query changes(WHERE clause) then my test class will fail(w/o SeeAllData=true). 

Its very difficult to dig into each and every trigger and finding for any SOQL queries and create that in my test class. For future maintenance also this is going to be tough. Is there a workaround for this?

Please suggest.

Thanks
Kumar
James LoghryJames Loghry
Kumar,

It sounds like you need to rearchitect your triggers.  Ten triggers on a single object is asking for trouble.  You should look at moving all your logic into an apex class that handles triggers. See the following for an excellent example of how you could do this: https://github.com/kevinohara80/sfdc-trigger-framework

Once you do that, come back to your unit test and remove the SeeAllData dependency.  You should be able to a single or multiple records that will effectively test your code.  If not, then you still have some basic issue with your logic.  

In other words, you should never need to use the SeeAllData=true annotation in your unit test.  It's a relic from the past and should be removed from any unit tests.