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
Adil Shaik (HYD)Adil Shaik (HYD) 

Could you help me in writing the test class for before insert trigger.

trigger PositionTrigger on Position__c (before insert,before update, before delete) {
    if(trigger.isBefore && (trigger.isInsert || trigger.isUpdate)){
        PositionTrigger_Handler.RecordTypeUpdate(Trigger.New);
    }
}
=============
public class PositionTrigger_Handler {
    Public Static void RecordTypeUpdate(List<Position__c> lstpos){
        Map<string,id> MappingRec = new Map<string, id>();
        List<RecordType> lstRec = [Select id, Developername from RecordType where SObjectType = 'Position__c'];
        for(RecordType rec: lstRec){
            MappingRec.put(rec.DeveloperName, rec.Id);
        }
        for(Position__c pos : lstpos){
            if(pos.Phase__c == 'Phase 1'){
                pos.RecordTypeId = MappingRec.get('Phase_1');
            }
            if(pos.Phase__c == 'Phase 2'){
                pos.RecordTypeId = MappingRec.get('Phase_2');
            }
            if(pos.Phase__c == 'Phase 3'){
                pos.RecordTypeId = MappingRec.get('Phase_3');
            }
        }
    }
}
=============================
@isTest
public class PositionTrigger_Handler_Test {
    
    Static testmethod void TestTrigger (){
        List<Position__c> lstpos = New List<Position__c> ();
        try{
            Position__C pos1 = New Position__C(Name='Test1', Amount__c=500, Phase__c='Phase 1');
            Position__C pos2 = New Position__C(Name='Test2', Amount__c=500, Phase__c='Phase 2');
            Position__C pos3 = New Position__C(Name='Test3', Amount__c=500, Phase__c='Phase 3');
            lstpos.add(pos1);
            lstpos.add(pos2);
            lstpos.add(pos3);
            insert lstpos;   
        }
        catch(DmlException e){
            system.debug('the following exception has occured: '+e.getMessage());
        }
        
    }
}
Best Answer chosen by Adil Shaik (HYD)
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Adil,

You need to compare the recordtype id that is assigned from the trigger and expected value.
It covers 100% trigger and handler apex class as well
 
@isTest
public class PositionTrigger_Handler_Test {
    
    Static testmethod void TestTrigger (){
        List<Position__c> lstpos = New List<Position__c> ();
        try{
            Position__C pos1 = New Position__C(Name='Test1', Phase__c='Phase 1');
            Position__C pos2 = New Position__C(Name='Test2', Phase__c='Phase 2');
            Position__C pos3 = New Position__C(Name='Test3',Phase__c='Phase 3');
            lstpos.add(pos1);
            lstpos.add(pos2);
            lstpos.add(pos3);
            insert lstpos;  
            Map<string,id> MappingRec = new Map<string, id>();
       
            List<RecordType> lstRec = [Select id, Developername from RecordType where SObjectType = 'Position__c'];
        for(RecordType rec: lstRec){
            MappingRec.put(rec.DeveloperName, rec.Id);
        }
           system.assertEquals(pos1.RecordTypeId, MappingRec.get('Phase_1'));
            system.assertEquals(pos2.RecordTypeId, MappingRec.get('Phase_2'));
            system.assertEquals(pos3.RecordTypeId, MappingRec.get('Phase_3'));
        }
        catch(DmlException e){
            system.debug('the following exception has occured: '+e.getMessage());
        }
        
    }
}

If this solution helps, Please mark it as best answer.

Thanks,
 

All Answers

Sai PraveenSai Praveen (Salesforce Developers) 
Hi Adil,

You need to compare the recordtype id that is assigned from the trigger and expected value.
It covers 100% trigger and handler apex class as well
 
@isTest
public class PositionTrigger_Handler_Test {
    
    Static testmethod void TestTrigger (){
        List<Position__c> lstpos = New List<Position__c> ();
        try{
            Position__C pos1 = New Position__C(Name='Test1', Phase__c='Phase 1');
            Position__C pos2 = New Position__C(Name='Test2', Phase__c='Phase 2');
            Position__C pos3 = New Position__C(Name='Test3',Phase__c='Phase 3');
            lstpos.add(pos1);
            lstpos.add(pos2);
            lstpos.add(pos3);
            insert lstpos;  
            Map<string,id> MappingRec = new Map<string, id>();
       
            List<RecordType> lstRec = [Select id, Developername from RecordType where SObjectType = 'Position__c'];
        for(RecordType rec: lstRec){
            MappingRec.put(rec.DeveloperName, rec.Id);
        }
           system.assertEquals(pos1.RecordTypeId, MappingRec.get('Phase_1'));
            system.assertEquals(pos2.RecordTypeId, MappingRec.get('Phase_2'));
            system.assertEquals(pos3.RecordTypeId, MappingRec.get('Phase_3'));
        }
        catch(DmlException e){
            system.debug('the following exception has occured: '+e.getMessage());
        }
        
    }
}

If this solution helps, Please mark it as best answer.

Thanks,
 
This was selected as the best answer
ravi soniravi soni
hi Adil,
try below test class with 100% coverage
@isTest
public class PositionTrigger_Handler_Test {
    
    private Static testmethod void createPosition(){
        List<Position__c> lstPosition = New List<Position__c> ();
        
		  for(integer i=1; i<=3; i++){
			Position__C pos = New Position__C(Name='Test ' +i, 
			                                  Phase__c='Phase ' + i);
/* Add all the required fieds by comma seprate*/											  
			  lstPosition.add(pos);
		  }
		  test.startTest();
               if(lstPosition.size() > 0){
			      insert lstPosition;  	   
			   }
		  test.stopTest();
		  
		  
		  
            
        
    }
}

don't forget to mark it as best answer if it helps you.
Thank you