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
Shruthi GM 4Shruthi GM 4 

hi,how to write test class for the below trigger:-

trigger Intervieee on Interviewer__c (after Update)

{

Set<Decimal> setPhoneNumber = new Set<Decimal>();
for(Interviewer__c i:trigger.new)
{
   if(i.Mobile_no__c != null)
{
// As per your current logic you are checking old value not new
   Interviewer__c invSet=Trigger.oldMap.get(i.Id);
   setPhoneNumber.add(invSet.Mobile_no__c);

}

    }
    if( setPhoneNumber.size() > 0 )
    {
    List<Candidates__c> canList = [select Name,Phone_Number__c,Qualification__c,Rating__c,Review_comments__c,Result__c,Mobile_no__c
              from Candidates__c where Mobile_no__c = :setPhoneNumber ];

                                         

        Map<Decimal ,Candidates__c> mapPhoneWiseCand = new Map<Decimal ,Candidates__c>();

        for( Candidates__c cand : canList )

        {

            mapPhoneWiseCand.put( cand.Mobile_no__c , cand );

        }

         

        list<Candidates__c> updatedcand=new list<Candidates__c>();

        for(Interviewer__c i:trigger.new)

        {

            if(i.Mobile_no__c != null)

            {

                Interviewer__c invSet=Trigger.oldMap.get(i.Id);

                if( mapPhoneWiseCand.containsKey( invSet.Mobile_no__c ) )

                {

                    Candidates__c canList1 = mapPhoneWiseCand.get(invSet.Mobile_no__c);

                     

                    canList1.Name=i.CName__c;

                    canlist1.Phone_Number__c=i.Phone_Number__c;

                    canlist1.Qualification__c=i.Candidate_s_qualification__c;

                    canlist1.Rating__c=i.Rating__c;
                    canlist1.Review_comments__c=i.Review_Comments__c;

                    canlist1.Result__c=i.Result__c;

                    canlist1.Mobile_no__c=i.Mobile_no__c;

                     

                    updatedcand.add(canList1);
                }

            }

        }

        if(updatedcand.size() > 0 )

        {

            try{

                system.debug('Before update');

                update updatedcand;

                system.debug('After update');

           }

            catch(Exception e){}

             

        }  

         

    }  

}

Please help.
Soundar Rajan PonpandiSoundar Rajan Ponpandi
Hi Shruthi,

Please follow below salesforce Best Practice for Test Classes always
 
1. Test class must start with @isTest annotation.
2. Test environment support @testVisible , @testSetUp .
3. Unit test is to test particular piece of code working properly or not .
4. To deploy to production at-least 75% code coverage is required
5. System.debug statement are not counted as a part of apex code limit.
6. Test method and test classes are not counted as a part of code limit
7. We should not focus on the  percentage of code coverage ,we should make sure that every use case should covered including positive, negative,bulk and single record .
8. Test class should be annotated with @isTest .
9 . @isTest annotation with test method  is equivalent to testMethod keyword .
10. Test method should static and no void return type .
11. Test method can not be used to test web-service call out . Please use call out mock .
12. You can't  send email from test method.
13.User, profile, organization, AsyncApexjob, Corntrigger, RecordType, ApexClass, ApexComponent ,ApexPage we can access without (seeAllData=true) .
14. @testSetup to create test records once in a method  and use in every test method in the test class .
15. Maximum number of test classes run per 24 hour of period is  not grater of 500 or 10 multiplication of test classes of your organization.
16. As apex runs in system mode so the permission and record sharing are not taken into account . So we need to use system.runAs to enforce record sharing .
17. Every test to runAs count against the total number of DML issued in the process .



Regards,

Soundar.
mohdAnasmohdAnas

Hi,

insert the Candidate object record first,
then  Insert the interviewer record

then update the Mobile_no__c of the interview records

Note: the Mobile_no__c of the candidate should match the Mobile_no__c of interviewer record when first entered.
But when updating the interviewer, u just need to update Mobile_no__c in already inserted interviewer records and your trigger will be covered

 

Shruthi GM 4Shruthi GM 4
hi,

this is how I have written the test class:-

@istest

public class Intervieeetest

{

    public static testmethod void testvalidate()

    {


        list<Candidates__c> candis=new list<Candidates__c>();

 
        //insertion of Candidate record
        Candidates__c cand=new Candidates__c();
        cand.Name='shruthi';
        cand.Qualification__c='BE';
        cand.Review_comments__c='Keep it up';
        cand.Rating__c = 'A';
        cand.Result__c='Cleared';
        cand.Mobile_no__c= 0123456789;
        candis.add(cand);
        insert candis;

 
        //insersion of Interview record
        Interviewer__c interview = new Interviewer__c ();
        interview.Name='Testing';
        interview.Result__c='Pass';
        interview.Mobile_no__c = 0123456789;
        interview.CName__c='preethi';
        insert interview;
        

 

        Test.StartTest();
        interview.Name='Testing';
        interview.CName__c='preethi';
        interview.Mobile_no__c = 0231456987;
        update interview;
        Test.StopTest();

         

    }

 

}
But facing this error!!!!!
System.DmlException: Insert failed. First exception on row 0; first error: DUPLICATE_VALUE, duplicate value found: <unknown> duplicates value on record with id: <unknown>: []
mohdAnasmohdAnas
it is possible that candidate or the interviewer with these values already exist in your org
The error message isnt sufficiant to trace the exact error 
try by just updating the mobile no 
 
comment these lines 
interview.Name='Testing';
interview.CName__c='preethi';

and kindly let me know the line number from where the error is generated.
Do report the code in the line please
   
Candidates__c cand=new Candidates__c();
        cand.Name='shruthi';
        cand.Qualification__c='BE';
        cand.Review_comments__c='Keep it up';
        cand.Rating__c = 'A';
        cand.Result__c='Cleared';
        cand.Mobile_no__c= 0123456789;
        candis.add(cand);
        insert candis;

 
        //insersion of Interview record
        Interviewer__c interview = new Interviewer__c ();
        interview.Name='Testing';
        interview.Result__c='Pass';
        interview.Mobile_no__c = 0123456789;
        interview.CName__c='preethi';
        insert interview;




try taking dummy values