0

For learning purpose, I was creating a trigger with following functionality :

There is a candidate object with a checkbox "Active" , Trigger functionality is to create a new record in "Review" object whenever Active becomes True.

This is the code of Trigger :

trigger create_review_on_active on Candidate__c (after update) {
    //Creating a list of new record of Review__c
    list <Review__c> newrecord = new list<Review__c>();

    for (Candidate__c objacc : Trigger.new){
        if (objacc.active__c == True)
        {
            Review__c objectr = new Review__c();
            objectr.name = 'Tejender Review'; // Just Creating record with fixed name for test Purpose. (Please suggest a way to use candidate name here.)
            newrecord .add(objectr);
 }

    insert newrecord ;
}
}

To Test the Above code, Following is the Testclass :

 @isTest
    public class Candidate_Review {

        @isTest static void candidate_review_test(){
        Candidate__c necan = new Candidate__c(name = 'TMJ');

        insert necan;


            Test.startTest();
            necan.Active__c = True;

            //At this point , i am not able to understand , how to Test whether the trigger is working fine or not ?

            Test.stopTest();


        }
    }

Question :
As per my knoweldge, for test class it is good to create a sample data so that i have created "necan" in test class but i am not able to understand how should i verify the new record of Review__c after Active__c becomes true and how should i get the proper code coverage.

Please suggest any solution so that can i verify this trigger using test class and also please provide your expert comments about any good practice that should have been used in above codes as i am a beginer to APEX. :)

Thanks in advance :)

1

The trigger you're using is fired when a Candidate__c record is updated.

In your current trigger, when the field active__c of your Candidate__c record is set to true, you create a new Review__c record.

So first, you have to update your Candidate__c record in your test class to fire the trigger. Like this:

Test.startTest();
necan.Active__c = True;

update necan;

Test.stopTest();

Your trigger will be fully covered with this.

But, in your test, you have to verify that your code is working with asserts. So you have to add this at the end of the test:

System.assertEquals(1, [select count() from Review__c where Name = 'Tejender Review']);

EDIT

To cover all the cases of your trigger, you have to add another method in your test class. In this method, you have to set the Active__c field of your Candidate__c record to false. Then verify that there is no Review__c record created.

6
  • Thanks Martin, I have added the above code and then executed the code using [Run Test] on developer anonymous window but I can neither find a log nor any code coverage. Code coverage is showing as none. May be my question is very basic because I am a beginner but can you please help or suggest any link where I can read the whole process to test a trigger and get code coverage. Thanks – Tejender Mohan Feb 9 '17 at 10:43
  • Did you check the code coverage on your trigger? Not on the test class. – Martin Lezer Feb 9 '17 at 10:58
  • Hi, Just checked on trigger. Code Coverage 100% (7/7) :) Thanks But what is the purpose of adding code coverage drop down list on developer window ? any idea ? – Tejender Mohan Feb 9 '17 at 11:07
  • Hi. You can check your code coverage on the developer console. But first you have to open the file. If you open your trigger on the developer console, you will be able to check its code coverage. – Martin Lezer Feb 9 '17 at 11:12
  • I edited my answer to help you check all the cases of your trigger. – Martin Lezer Feb 9 '17 at 11:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.