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
Peter KayePeter Kaye 

Code Coverage Problem - Simple Trigger

I am trying to rehearse trigger deployment with a simple test example.  Here is the trigger and the test code.
 
trigger HelloWorldTrigger on Book__c(before insert) {

    Book__c[] books = Trigger.new;
    MyHelloWorld.applyDiscount(books);
}


@isTest
    private class HelloWorldTestClass {
        static testMethod void validateHelloWorld() {
           Book__c b = new Book__c(Name='Behind the Cloud', Price__c=100);
           System.debug('Price before inserting new book: ' + b.Price__c);
     
           // Insert book
           insert b;
         
           // Retrieve the new book
           b = [SELECT Price__c FROM Book__c WHERE Id =:b.Id];
           System.debug('Price after trigger fired: ' + b.Price__c);
     
           // Test that the trigger correctly updated the price
           System.assertEquals(90, b.Price__c);
        }
    }
I have successfully deployed the trigger but it it fails to function because of  insufficient code coverage.

This is the error message.

User-added image
This is the information from the developer console
User-added image
The trigger coverage is 100% which I thought was enough.

I am obviously missing something important so I'd apprecaite any help to:
  • Identify what code I need to add to the test class above to meet the coverage requirements.
  • Explain how I can tell from the developer console whether  tests for other triggers will pass coverage requirements.
Thanks.

 
Best Answer chosen by Peter Kaye
Zachery EngmanZachery Engman
Hi Peter - from your screenshot, if I am understanding correctly, it is showing only 1 component as part of the deployment.  

You need to deploy the test class to the destination org along with the trigger as part of the changeset.  Are you doing that or is your test class only in the source org ?  If so that is your issue as your test class looks fine.

All Answers

Zachery EngmanZachery Engman
Hi Peter - from your screenshot, if I am understanding correctly, it is showing only 1 component as part of the deployment.  

You need to deploy the test class to the destination org along with the trigger as part of the changeset.  Are you doing that or is your test class only in the source org ?  If so that is your issue as your test class looks fine.
This was selected as the best answer
Peter KayePeter Kaye
Brilliant Zachery - that was exactly my problem.  For some reason I thought the test class was only required in the sandbox and that no deployment would take place if the tests failed in the sandbox.  Once I added the Apex test class deployment worked fine.  Thanks very much for this, my trigger show is on the raod again !