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
alynlinalynlin 

How to increse test coverage in apex trigger? (coverage is 0%)

Hi, I am learning salesforce and apex coding, and got some problems.

 

For example, code like this: (The function is, Tag the record with latest date "Most Recent Record", and other record not tagged.)

 

 

------------------------------------------------------------------------------------------------------------------------

trigger LatestEvaluationHistory on Evaluation_History__c (after delete, after insert, after update) {
        
        List<Evaluation_History__c> ctxsToUpdate = new List<Evaluation_History__c>{};    
        Set<Id> emtoupdate = new Set<Id> ();
        boolean firstRecord = true;    
        
        if (Trigger.isInsert || Trigger.isUpdate ) {
            for (Evaluation_History__c c: Trigger.New) {
                emtoupdate.add(c.Job_Name__c);
            }
        }
        
        else {
            for (Evaluation_History__c c: Trigger.Old) {
                emtoupdate.add(c.Job_Name__c);
            }
        }
        
        if(ctxsToUpdate != null && !ctxsToUpdate.IsEmpty())
        {
        for(Evaluation_History__c ctx : [Select Id, Most_Recent_Record__c, Effective_Date__c
            from Evaluation_History__c where Job_Name__c =: emtoupdate ORDER BY Effective_Date__c DESC])
            {
                if(firstRecord)
                {
                    ctx.Most_Recent_Record__c = True;
                    firstRecord = false;    //System.debug(LoggingLevel.INFO,'a'+ctx.Id);
                }
                else
                {
                    ctx.Most_Recent_Record__c = False;    //System.debug(LoggingLevel.INFO,'b'+ctx.Id);
                }
                    
                ctxsToUpdate.add(ctx);
            }
        
        update ctxsToUpdate;
        }
}

------------------------------------------------------------------------------------------------------------------------

 

This code can work in Sandbox, but cannot deploy into production because the test coverage is 0%.

So Who can teach me how to increase the test coverage???

 

Thanks a lot.

I am really torturing in this part.

 

BaLab2aBaLab2a

Can yu please post the test method for this trigger??

alynlinalynlin

Hi, BaLab2a:

 

I don't quite understand about test method either...

 

This logic is all built inside the trigger, do i have to create a new test method? If so, how to?

BaLab2aBaLab2a

Hi,

 

Test methods are required to deploy Apex to a production environment. They are also required if your code is going to be packaged and placed on Force.com AppExchange. The test methods must provide at least 75% code coverage. Think of a test method as 'Apex code that tests other Apex code.' Code coverage is calculated by dividing the number of unique Apex code lines executed during your test method execution by the total number of Apex code lines in all of your trigger and classes.

 

please check the following link.

 

http://wiki.developerforce.com/index.php/An_Introduction_to_Apex_Code_Test_Methods

 

Thanks,

 

BaLa

pbattissonpbattisson

alynlin

 

You need to write a test class that performs the necessary DML operation to exercise the code in your trigger. I wrote a post on Teach Me Salesforce about this a few weeks back. My recommendation would be to read that and ask questions as it should cover anything you need to know.

 

Paul

du-scodouglasdu-scodouglas

Alynlin,

 

Create a Apex Class and mark it as @IsTest

 

Put a static method in that class, this method should create and insert all the objects you need to "test" a scenario or user case where this trigger would fire.

 

So you could get coverage by inserting a Evaluation_History__c, deleting a Evaluation_History__c, insert another (to cause a need to update the Most Recent Record)

 

Then simply run this test class by navigating to it in your SF instance under Setup -> Develop -> Apex Classes

alynlinalynlin

Hi, thank you all, now I understand the relation between trigger and class.

 

I wrote a test method, and now the test coverage rate is 82%.

I think I already covered all my needs including insert, delete, update.

 

But since now it is already > 75%, so I am gonna leave it there.

So sometimes it's hard to get 100%?

pbattissonpbattisson

It can be difficult to hit 100% all the time, but I would recommend striving for it as often as possible. Although it should be remembered that the aim is to test the code fully (ensuring that every scenario has been tested in code) and not just aim to get 100% of the code executed without being asserted on.