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
Jean-Se DoraisJean-Se Dorais 

0% Code Coverage!?!

Hello all,

I've got a 'FundingAwardTrigger' on the FundingAward object (part of the Grantmaking package).  When I run the test in Sandbox, I get 100% coverage.

When I validate the Inbound Change Set in Prod, I get the following error:

Code Coverage Failure
The following triggers have 0% code coverage. Each trigger must have at least 1% code coverage.
FundingAwardTrigger

Here's my test class:
 
@isTest
private class FundingAwardTriggerTest {

    @isTest static void testTriggerOnInsert() {
        // Create a test record for the insert operation
        FundingAward testAwardInsert = new FundingAward(Name = 'Test Insert', Amount = 100);

        // Start the test context
        Test.startTest();

        // Perform the insert operation
        insert testAwardInsert;

        // Stop the test context
        Test.stopTest();

        // Retrieve the inserted record and assert the result
        testAwardInsert = [SELECT Currency_Text__c FROM FundingAward WHERE Id = :testAwardInsert.Id];
        System.assertEquals('One Hundred', testAwardInsert.Currency_Text__c, 'Trigger did not work as expected on insert');
    }

    @isTest static void testTriggerOnUpdate() {
        // Create and insert a record for the update operation
        FundingAward testAwardUpdate = new FundingAward(Name = 'Test Update', Amount = 100);
        insert testAwardUpdate;

        // Start the test context
        Test.startTest();

        // Perform the update operation
        testAwardUpdate.Amount = 200;
        update testAwardUpdate;

        // Stop the test context
        Test.stopTest();

        // Retrieve the updated record and assert the result
        testAwardUpdate = [SELECT Currency_Text__c FROM FundingAward WHERE Id = :testAwardUpdate.Id];
        System.assertEquals('Two Hundred', testAwardUpdate.Currency_Text__c, 'Trigger did not work as expected on update');
    }

 		@isTest static void testBulkOperation() {
        // Create multiple records for testing bulk operation
        List<FundingAward> awards = new List<FundingAward>();
        for (Integer i = 1; i <= 10; i++) {
            awards.add(new FundingAward(Name = 'Bulk Test Award ' + i, Amount = i * 100));
        }

        // Start the test context
        Test.startTest();

        // Perform bulk insert
        insert awards;

        // Update the records for bulk update test
        for (Integer i = 0; i < awards.size(); i++) {
            awards[i].Amount += 1000;
        }

        // Perform bulk update
        update awards;

        // Stop the test context
        Test.stopTest();

        // Optional: Add assertions here to validate the trigger's behavior on bulk operations
    }

}
Why would my test class show 100% in sandbox but 0% in prod?

Any tips appreciated!
 
Best Answer chosen by Jean-Se Dorais
Jean-Se DoraisJean-Se Dorais
Solved.  The FundingAwardTriggerTest test class was omitted from my outbound change set.  Including it solved the issue!

All Answers

HarshHarsh (Salesforce Developers) 
Hi Jean,
  • sandbox and production instances have totally different data, so your tests will run on different data as well.
 
  • The way around this is to create all new records in the beginning of your test class.  Make sure not to use the SeeAllData=true annotation!
 
  • Also - your production org might have certain configurations that may be generating errors in your test class and lowering its coverage.  These could be validation rules, custom settings, workflows, etc.  
 
Hope this helps!
Check similar references below.

https://developer.salesforce.com/forums/?id=9062I000000DIXqQAO 

https://trailhead.salesforce.com/trailblazer-community/feed/0D54S00000A8JLJSA3 


Please mark it as Best Answer if the above information was helpful.

Thanks.



Important Update - last chance
User-added image
Heads up — this is your last chance to get your Trailblazer account set up and connected to your forums discussions on this site.

Please take these steps before November 30, 2023, 11:59 p.m. PT to ensure your contributions follow you to the Trailblazer Community:
  1. If you’re not already a member of the Trailblazer Communitysign up for a Trailblazer account using the same login email address associated with your Developer Discussion Forums account. This is crucial.
  2. If you already have a Trailblazer account, and it’s using a different email address from your Developer Discussion Forums account, we recommend that you log in to the Trailblazer Community and connect your forums email address to your Trailblazer account.
Find more info & support
We know that moving platforms can be a hassle, so we created a special forums users group, the Migration Support Hub, in the Trailblazer Community where you can find other forums users and get training videos, the latest information, and assistance.

We want to thank you for all of your time, energy, and contributions made here in the Salesforce Discussion Forums.
We’ll see you in the Trailblazer Community!

Sincerely,
The Forums Support Team
Jean-Se DoraisJean-Se Dorais
Solved.  The FundingAwardTriggerTest test class was omitted from my outbound change set.  Including it solved the issue!
This was selected as the best answer