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
NANCY1NANCY1 

Help needed in writing a Test class for a Trigger??

Hi All,

 

I have a trigger for which i have written a test class.. but that covers only 33% code area.. how can i make it more then 75%...

 

My Apex Trigger:

 

trigger proposalstatus on Proposal__c (after update, after insert, after delete)

    if (Trigger.isDelete){
        /* This will always have only one Id*/
        List < Id > empmasterIds = new List < Id >();
        List < Id > proposalIds = new List < Id >();
   
        for ( Proposal__c  c: Trigger.old)
        {
            proposalIds.add( c.Id );
            empmasterIds.add( c.RMG_Employee_Code__c);
        }
       
        /* This will contain the aggregated proposal status of the employee*/
        List<RMG_Employee_Master__c> opps = [select id, Proposal_Status__c, Temp_Proposal_Status__c from RMG_Employee_Master__c where id in :empmasterIds];
   
        /* Mapping between employeeId and employee*/
        Map < Id ,RMG_Employee_Master__c > empmap = new Map < Id , RMG_Employee_Master__c >();
        for ( RMG_Employee_Master__c  a : opps   )
        {
            empmap.put( a.Id, a);
        }

        /* List of employees to be updated. This will have only one employee - the current employee.*/
        List < RMG_Employee_Master__c > EmpToUpdate = new List < RMG_Employee_Master__c >();
   
        /* Get the status of the new proposal */
        for(Proposal__c c: Trigger.old)
        {
           system.debug('Inside Proposal Loop 2');
           /* Fetch the current employee */
           RMG_Employee_Master__c ac = empmap.get( c.RMG_Employee_Code__c );        
           if (ac == null)
           {   
               continue;
           }
       
           system.debug('----------->'+c.Status__c);
           /* Get all proposals for this employee and mark them as closed. */
           String futureProposalStatus = 'Unassigned';
           List<Proposal__c> proObj = [select Id,Status__c,RMG_Employee_Code__c from Proposal__c where Id !=:c.Id and RMG_Employee_Code__c =:ac.Id ];
           for(Proposal__c pc: proObj )
           {
                if ((pc.Status__c == 'Proposed') && (futureProposalStatus == 'Proposed')){
                    futureProposalStatus = 'AUP';
                } else if ((pc.Status__c == 'Proposed')
                        || (pc.Status__c == 'Blocked')
                        || (pc.Status__c == 'Selected-New')
                        || (pc.Status__c == 'Selected-Extended')){
                    futureProposalStatus = pc.Status__c;
                }
           }//for
           ac.Proposal_Status__c = futureProposalStatus;
           ac.Temp_Proposal_Status__c = futureProposalStatus;
           EmpToUpdate.add(ac);
           system.debug('Inside Proposal Loop 2: '+futureProposalStatus);
         }//For Trigger.old
         upsert EmpToUpdate;      
    } else {
        /* This will always have only one Id*/
        List < Id > empmasterIds = new List < Id >();
        List < Id > proposalIds = new List < Id >();

        for ( Proposal__c  c: Trigger.New )
        {
            proposalIds.add( c.Id );
            empmasterIds.add( c.RMG_Employee_Code__c);
        }
   
        /* This will contain the aggregated proposal status of the employee*/
        List<RMG_Employee_Master__c> opps = [select id, Proposal_Status__c,Temp_Proposal_Status__c from RMG_Employee_Master__c where id in :empmasterIds];
   
        /* Mapping between employeeId and employee*/
        Map < Id ,RMG_Employee_Master__c > empmap = new Map < Id , RMG_Employee_Master__c >();
        for ( RMG_Employee_Master__c  a : opps   )
        {
            empmap.put( a.Id, a);
        }

        /* List of employees to be updated. This will have only one employee - the current employee.*/
        List < RMG_Employee_Master__c > EmpToUpdate = new List < RMG_Employee_Master__c >();
   
        /* Get the status of the new proposal */
        for(Proposal__c c: Trigger.New)
        {
            /* Fetch the current employee */
            RMG_Employee_Master__c ac = empmap.get( c.RMG_Employee_Code__c );        
            if ( ac == null )
            {   
                  continue;
            }
       
            /* If the new proposal's status is not closed, then set the Aggregated proposal status as the status
             of the new Proposal, else mark it as unassigned. */
       
            If (c.Status__c != 'Closed')
            {
               ac.Proposal_Status__c = c.Status__c;   
               ac.Temp_Proposal_Status__c = c.Status__c;
               EmpToUpdate.add( ac );
            }
            else
            {
               ac.Proposal_Status__c = 'Unassigned';
               ac.Temp_Proposal_Status__c = 'Unassigned';
               EmpToUpdate.add( ac );
            }
       
            /* If the new Proposal's status is equal to 'Blocked' or 'Selected' then close rest of the proposals. */
            if((c.Status__c == 'Selected-New') || (c.Status__c == 'Blocked') || (c.Status__c == 'Selected-Extended'))
            {
                system.debug('----------->'+c.Status__c);
                /* Get all proposals for this employee and mark them as closed. */
                List<Proposal__c> proObj = [select Id,Status__c,RMG_Employee_Code__c from Proposal__c where Id !=:c.Id and RMG_Employee_Code__c =:ac.Id ];
                for(Proposal__c pc: proObj )
                {
                   system.debug('BEFORE STATUS--->'+pc.Status__c);
         
                   if ((pc.Status__c != 'Closed')
                       && (pc.Status__c != 'Likely Extension')
                       && (pc.Status__c != 'Blocked')
                       && (pc.Status__c != 'Selected-New')
                       && (pc.Status__c != 'Selected-Extended')){            
                       pc.Status__c = 'Closed';
                       system.debug('AFTER STATUS--->'+pc.Status__c);
                       update pc;
                   }//if
                }//for
             }//if
         }//For Trigger.New
         upsert EmpToUpdate;      
     }//Else
}//End of Trigger

Best Answer chosen by Admin (Salesforce Developers) 
Ritesh AswaneyRitesh Aswaney

Hey, I can see you have two branches depending on the status of Proposed. Your test case covers one of them.

 

You could create another Proposal record, to meet the other condition, which would send the test class down the other branch of the if loop.

 

Basically, the objective of writing test classes, is to create data which can send the test class down the different logical paths in your class / trigger.

All Answers

NANCY1NANCY1

Test class i have for the above trigger is:

 

@isTest
private class TestProposalStatus
{

   static testMethod void testTrigger()
   {
      RMG_Employee_Master__c opp=new RMG_Employee_Master__c();
      insert opp;
      Proposal__c oProp=new Proposal__c(
                                     Status__c='Proposed',
                                     RMG_Employee_Code__c=opp.id);
      insert oProp;
      update oProp;
      Proposal__c newOprop=[select Name, Status__c
                                         from Proposal__c where id = :oProp.id];
      System.assertEquals('Proposed', newOprop.Status__c);
   }
}

Ritesh AswaneyRitesh Aswaney

Hey, I can see you have two branches depending on the status of Proposed. Your test case covers one of them.

 

You could create another Proposal record, to meet the other condition, which would send the test class down the other branch of the if loop.

 

Basically, the objective of writing test classes, is to create data which can send the test class down the different logical paths in your class / trigger.

This was selected as the best answer