+ Start a Discussion
ranga babu vangalapudi 2ranga babu vangalapudi 2 

how to cover maps in test classes that are written in Triggers

iam unable to cover maps in test classes
 Below is the Trigger:
trigger JKT_triggerOnBeatTSEMapping on Beat_TSE_Mapping__c (before insert,before update,after update) {
    
    if (Trigger.IsAfter && Trigger.IsUpdate){
        
        Map<String, String> mapOldNewValue = New Map<String, String>();
        Map<String, String> mapOldNewValue1 = New Map<String, String>();
        Map<String, String> mapOldNewValue2 = New Map<String, String>();
        Map<String, String> mapOldNewValue3 = New Map<String, String>();
        Map<String, String> mapOldNewValue4 = New Map<String, String>();
        Map<String,String> mapOldNewValue5 = New Map<String,String>();
        List<string> lstofBeatNames = New List<string>();
        List<string> lstofBeatCodes = New List<string>();
        
        for(Beat_TSE_Mapping__c oldTseMasterRecord : Trigger.Old){
            if(Trigger.NewMap.get(oldTseMasterRecord.Id).Name != oldTseMasterRecord.Name && oldTseMasterRecord.Name != null){
                mapOldNewValue.put(oldTseMasterRecord.Name, Trigger.NewMap.get(oldTseMasterRecord.Id).Name);
            }
            
        }
        for(Beat_TSE_Mapping__c oldTseMasterRecord1 : Trigger.Old){
            if(Trigger.NewMap.get(oldTseMasterRecord1.Id).Beat_Name__c != oldTseMasterRecord1.Beat_Name__c && oldTseMasterRecord1.Beat_Name__c != null){
                mapOldNewValue1.put(oldTseMasterRecord1.Beat_Name__c, Trigger.NewMap.get(oldTseMasterRecord1.Id).Beat_Name__c);
            }
            
        }
        for(Beat_TSE_Mapping__c newTseMasterRecord2 : Trigger.new){  
            if(newTseMasterRecord2.TSE_Territory_Name__c !=trigger.oldmap.get(newTseMasterRecord2.ID).TSE_Territory_Name__c ) {
                lstofBeatNames.add(newTseMasterRecord2.Beat_Name__c);
                lstofBeatCodes.add(newTseMasterRecord2.Name);
                mapOldNewValue2.put(trigger.oldmap.get(newTseMasterRecord2.ID).TSE_Territory_Name__c , newTseMasterRecord2.TSE_Territory_Name__c); 
                mapOldNewValue3.put(trigger.oldmap.get(newTseMasterRecord2.ID).TSE_ID__c , newTseMasterRecord2.TSE_ID__c);
                mapOldNewValue4.put(trigger.oldmap.get(newTseMasterRecord2.ID).TSE_Code__c , newTseMasterRecord2.TSE_Code__c);
                
                
            }
        }  
        list<Account> listAccountToUpdate = new list<Account>();
        for(Account objAccount : [SELECT Id,Beat_Code__c FROM Account WHERE Beat_Code__c IN: mapOldNewValue.keySet()] ){
            objAccount.Beat_Code__c = mapOldNewValue.get(objAccount.Beat_Code__c);
            listAccountToUpdate.add(objAccount);
        }
        list<Account> listAccountToUpdate1 = new list<Account>();
        for(Account objAccount1 : [SELECT Id,Beat_Name__c FROM Account WHERE Beat_Name__c IN: mapOldNewValue1.keySet()] ){
            //objAccount1.Beat_Name__c = mapOldNewValue1.get(objAccount1.Beat_Name__c);
            listAccountToUpdate1.add(objAccount1);
        }
        
        List<TSE_Master__c> OldTseCodes = New List<TSE_Master__c>();
        List<TSE_Master__c> NewTseCodes = New List<TSE_Master__c>();
        OldTseCodes = [SELECT Name,TSE_Territory_Name__c FROM TSE_Master__c WHERE  Id IN : mapOldNewValue4.keySet() LIMIT 1 ];
        NewTseCodes = [SELECT Name,TSE_Territory_Name__c FROM TSE_Master__c WHERE  Id IN : mapOldNewValue4.values() LIMIT 1];
        if(!OldTseCodes.isEmpty()){
            string OldTse = string.ValueOf(OldTseCodes[0].get('Name'));
            string NewTse = string.valueOf(NewTseCodes[0].get('Name'));
            mapOldNewValue5.put(OldTse, NewTse);
        }
        
        list<Account> listAccountToUpdate2 = new list<Account>();
        for(Account objAccount2 : [SELECT Id,Name,TSE_Code__c,Beat_Name__c,Beat_Code__c,TSE_ID__c FROM Account WHERE  Beat_Name__c IN : lstofBeatNames AND Beat_Code__c IN : lstofBeatCodes and TSE_Code__c != null]){      
            //objAccount2.TSE_Name__c = mapOldNewValue2.get(objAccount2.TSE_Name__c);
            //  objAccount2.TSE_ID__c =  mapOldNewValue3.get(objAccount2.TSE_ID__c);
            if(mapOldNewValue5.containsKey(objAccount2.TSE_Code__c) && mapOldNewValue5.get(objAccount2.TSE_Code__c) != null && mapOldNewValue5.get(objAccount2.TSE_Code__c) != ''){
                objAccount2.TSE_Code__c = mapOldNewValue5.get(objAccount2.TSE_Code__c);
                listAccountToUpdate2.add(objAccount2);
            }
        }
        
        
        if(listAccountToUpdate2.size()>0){
            update listAccountToUpdate2;
        }   
        DuplicateBeatNameCheck beatObj= new DuplicateBeatNameCheck();
        beatObj.dupchecker(Trigger.new ,'Update',Trigger.oldMap);
        beatObj.BeatNameValidation(Trigger.new,Trigger.oldMap);
    }
    
    if (trigger.IsBefore && Trigger.IsInsert){
        DuplicateBeatNameCheck beatObj= new DuplicateBeatNameCheck();
        beatObj.dupchecker(Trigger.new,'Insert',Trigger.oldMap);
       //beatObj.dupbeatnamechecker(Trigger.new);
        CustomBeatCode.autogenerateBeatcode(Trigger.new);        
    }        
}
======================================================================
This is the test class i written
@istest
public with sharing class JKT_BeatCodeconttrollerTest {
    static testMethod Void BeatCodeconttrollerMethod(){
         test.StartTest();          
         Profile prfile = [select Id,name from Profile limit 1];
         USer objUser = new User();
         objUser.Username = 'TestMyName@jkt.com';
         objUser.LastName = 'testuser';
         objUser.Email = 'test@gmail.com';
         objUser.Alias = 'test1';
         objUser.CommunityNickname = 'testcommunity';
         objUser.TimeZoneSidKey = 'America/Chicago';     
         objUser.LocaleSidKey = 'en_US';
         objUser.EmailEncodingKey = 'UTF-8';
         objUser.ProfileId =prfile.Id;
         objUser.Division = 'NORTH' ;
         objUser.LanguageLocaleKey = 'en_US';
         insert objUser ;
         Id RecordTypeId = Schema.SObjectType.Masters__c.getRecordTypeInfosByName().get('Cluster').getRecordTypeId();
         Masters__c masters=new Masters__c();
         //masters.Name='A-0320';
         masters.Cluster_Name__c='ANDHRA PRADESHTest';
         masters.RecordTypeid=RecordTypeId;
         insert masters;
        
        ClusterCode__c beatCustomCode=new ClusterCode__c();
         beatCustomCode.name='ANDHRA PRADESHTest';
         beatCustomCode.GEOGRAPHIC_STATE__c='ANDHRA PRADESHTest';
         beatCustomCode.Cluster_Abbr__c='AP';
         beatCustomCode.CountValue__c=10;
         insert beatCustomCode;
         
         ClusterCodeTL__c TlCustomCode=new ClusterCodeTL__c();
         TlCustomCode.name='ANDHRA PRADESHTest';
         TlCustomCode.GEOGRAPHIC_STATE__c='ANDHRA PRADESHTest';
         TlCustomCode.Cluster_Abbr__c='AP';
         TlCustomCode.CountValue__c=10;
         insert TlCustomCode;
         
         ClusterCodeTSE__c TseCustomCode=new ClusterCodeTSE__c();
         TseCustomCode.name='ANDHRA PRADESHTest';
         TseCustomCode.GEOGRAPHIC_STATE__c='ANDHRA PRADESHTest';
         TseCustomCode.Cluster_Abbr__c='AP';
         TseCustomCode.CountValue__c=10;
         insert TseCustomCode;
        
          TL_Master__c tl= new TL_Master__c();
         tl.Name='APTL999';
         tl.TL_Territory_Name__c='CENTRAL COASTALTest';
         tl.Cluster__c=masters.id;
         insert tl;
                 
        TSE_Master__c oldTse1 = new TSE_Master__c();
        oldTse1.Name = 'APTS999';
        oldTse1.TSE_Territory_Name__c = 'AMALAPURAMTest';
        oldTse1.TL_Code__c=tl.id;
        insert oldTse1;
         
        Beat_TSE_Mapping__c NewBte = new Beat_TSE_Mapping__c();
        NewBte.Beat_Name__c = 'Beat_1234';
        NewBte.Name = 'KABT9999';
        NewBte.TSE_Code__c = oldTse1.id;
        insert NewBte;
        List <Beat_TSE_Mapping__c> BeatName = new List <Beat_TSE_Mapping__c>();
        BeatName.add(NewBte);
         Test.setCurrentPage(Page.Beatcodegeneration); 
         PageReference pRef = Page.Beatcodegeneration;
        pRef.getParameters().put('id', NewBte.TSE_Code__c);
        pRef.getParameters().put('BeatCode', oldTse1.Name);
       
        Test.setCurrentPage(pRef);

        JKT_BeatCodeconttroller BeatObj=new  JKT_BeatCodeconttroller(new ApexPages.StandardController(NewBte));
         DuplicateBeatNameCheck newBEat = new DuplicateBeatNameCheck();
       BeatObj.Save();
         BeatObj.clonerequest='1';
         BeatObj.edit=false;
        // NewBte.TSE_Code__c = oldTse1.id;
         BeatObj.Cancel();
         JKT_BeatCodeconttroller.countCalculation(1);
         JKT_BeatCodeconttroller.countCalculation(20);
         JKT_BeatCodeconttroller.countCalculation(388);     
           
         pRef.getParameters().put('clustervalue',[SELECT Name FROM TSE_Master__c  Limit 1][0].name); 
         BeatObj.autoGeneratebitCode();
         newBEat.dupbeatnamechecker(BeatName);
        Test.stopTest();
    }
}

I checked in some sites and they used "system.assertEquals",  but iam not sure how it works.
 your help is greatly appreciated.