3

I have been struggling with this for 5 hours now and can't find a solution.

I am trying to push my code from the sandbox to production but when I try to validate the deployment in the production environment I am getting this error:

  • Code Coverage Failure Your organization's code coverage is 65%. You need at least 75% coverage to complete this deployment.

When I run all of my test classes in the sandbox I am at 80% coverage. I am uploading the apex classes, trigger, & test class in the package and they all test above the threshold. This is just an update to the code so I have been able to deploy to production in the past. Any help or suggestions would be greatly appreciated.


@isTest (SeeAllData=false)
    static void TestTrigger()
    {
        Account newAccount = new Account (name = 'Test',
        BillingCity ='TestCity',
        BillingCountry ='TestCountry',
        BillingStreet ='TestStreet',
        BillingPostalCode ='t3stcd3'
        );

        insert newAccount;

        if(newAccount.Id != null)
        {
            Contact newContact = new Contact (
            FirstName = 'xyzFirst',
            LastName = 'XyZLast',
            AccountId = newAccount.Id,
            Email = '[email protected]'
            );

            insert newContact;
        }


         return;
}
4
  • 1
    possible duplicate of Package test coverage way lower than actual
    – Novarg
    Dec 30, 2014 at 15:18
  • @TonyK - First place to start looking is the classes with the lowest total coverage and update the code to bring the coverage in compliance. Also a good time to ensure you are using assertions to ensure proper results are being obtained.
    – Eric
    Dec 30, 2014 at 17:17
  • return; is not used in a Test method. Move your closing bracket for your if(newAccount.Id != null) statement to before insert newContact; and just before the insert statement, you should have a test.startTest;
    – crmprogdev
    Dec 30, 2014 at 17:22
  • return; won't matter, it just ends the method since it is a void return. What does the trigger do that is causing the issues?
    – drakored
    Dec 30, 2014 at 17:31

3 Answers 3

3

First thing to check are your test cases. Here are the things I check when receiving this problem:

Each class is set to SeeAllData=false This is a big one, if your class is set to SeeAllData=true and you're relying on data from the org during the testing phase instead of building your own, it'll fail due to differences in the org. The best practice here is to build all of your test data individually in your test cases.

Make sure no try/catch blocks are allowing code to slip through. This can change the course of the test case if you're running try/catch in the test case but ignoring the catch section. Allowing the test to bypass like that will reduce the amount of code covered as well. Test classes are the one place to really not use try/catch religiously.

Make sure your test cases are using System.runAs and setting up a user in the proper profile. Certain sections of your code might be skipped if the running user doesn't have access to specific fields, objects, etc. It is best to always configure a user in the test class with the proper profile to ensure this issue doesn't happen. This can also happen with feature licenses like Knowledge where certain objects aren't available unless that feature license is enabled at the user level. Usually that'll throw a specific error to that, but you could run into code that try/catches around it, and causes lowering of the test coverage levels.

The biggest thing is to remember that you're moving your data to a different org with a different user, different permissions, etc. Hopefully your testing and prod orgs are nearly in sync, but that isn't always the case. Comb the code and make sure nothing in there is relying on that other org.

2
  • I changed the test method to seealldata=false and created the objects I need but I am getting null exceptions now. The code seems simple but I can't get it to work
    – TonyK
    Dec 30, 2014 at 17:05
  • It sounds like your old test code was relying on data in the org, but something changed and caused it to not function anymore. Your test code looks good at a high level, but the trigger might be looking at a field you're not including in your test data. Follow through on the code and make sure all fields it is using/manipulating are filled in. If not, update the test, also you might want to update the trigger if it is throwing nullpointerexception over missing data (this might be one case where the test methods showed you a problem).
    – drakored
    Dec 30, 2014 at 17:36
1

In addition to all of the above, run all tests in PROD before deploying to see if code prior to deployment runs successfully with > 75% code coverage.

Such production code might no longer cover as much as before due to changes in the metadata environment done directly in PROD - such as workflow changes, formula field changes, or validation rule changes.

0

I would compare what's in your sandbox and what's in your production. And compare the tests that are actually being run.

You may have a class in your sandbox that has high code coverage, that you aren't deploying. For example open source utility code such as apex-lang.

Not the answer you're looking for? Browse other questions tagged .