+ Start a Discussion
Manju053Manju053 

Need help in Writing Test class for custom Object

Hello Everyone

I am trying to write single Test class for both Handler class and Trigger, i have written a Test class but i am not able to cover the code coverage, 
any help or suggestion will help a lot

QuotaTriggerHandler
 
public class QuotaTriggerHandler {
    
    public static set<Id> assignedtoSet =new Set<Id>();
    public static set<String> QuaterString =new Set<String>();
    public static set<String> YearString =new Set<String>();
    
    public static final string Quater_1='Q1';
    public static final string Quater_2='Q2';
    public static final string Quater_3='Q3';
    public static final string Quater_4='Q4'; 
    
    public static final string Jan='Jan';
    public static final string Feb='Feb';
    public static final string Mar='Mar';
    public static final string Apr='Apr';
    public static final string May='May';
    public static final string Jun='Jun';
    public static final string Jul='Jul';
    public static final string Aug='Aug';
    public static final string Sep='Sep';
    public static final string Oct='Oct';
    public static final string Nov='Nov';
    public static final string Dec='Dec';
    
    public static final string Null_String='null';
    public static final Integer Integr_Null_String=null;
    
    public static void dupQuotaError (List <Quota__c> newTrigQuo) {
        
        Id recordTypeId = Schema.SObjectType.Quota__c.getRecordTypeInfosByName().get('IS Executive').getRecordTypeId();
        
        //List of all Quota Records with Recordtpe inside sales target currently in the database    
        List <Quota__c> allQuo = [SELECT ID, Name, Assigned_To__c, Quater_Year__c, RecordTypeId, Month__c FROM Quota__c where RecordTypeId=:recordTypeId];
        
        //Iterate through new potential records
        for (Quota__c oneQuoTrig : newTrigQuo) {
            
            //Needed to iterate through all existing records which is contained in the allCon List
            for (Integer i = 0; i < allQuo.size(); i++)
                
                //If all conditions are met, there is a duplicate and thus, returns an error.
                if (oneQuoTrig.Assigned_To__c == allQuo[i].Assigned_To__c 
                    && oneQuoTrig.Quater_Year__c == allQuo[i].Quater_Year__c 
                    && oneQuoTrig.Month__c == allQuo[i].Month__c
                    && oneQuoTrig.RecordTypeId == recordTypeId) {
                        oneQuoTrig.addError('Duplicate Quota Detected! This record already exists.');
                    }
        }      
    }
    public static void UpdateDate(List<Quota__c> quotaList){
        for(Quota__c q: quotaList){
            
            string year = q.Quater_Year__c;
            Date dt= System.today();
            Integer day = 1;
            Integer Month= dt.month();
            Date Startday;  
            
            if(q.Quater__c==Quater_1){
                Month=1;
            }else if(q.Quater__c==Quater_2){
                Month=4;
            }else if(q.Quater__c==Quater_3){
                Month=7;
            }else if(q.Quater__c==Quater_4){
                Month=10;
            }
            
            Integer IntMonth=Integr_Null_String;
            integer ConvertMonth=Integr_Null_String;
            integer ConvertYear=Integr_Null_String;
            
            if(Month==1){
                IntMonth=1;
            }else if(Month==4){
                IntMonth=4;
            }else if(Month==7){
                IntMonth=7;
            }else if(Month==10){
                IntMonth=10;
            }
            
            if(IntMonth!=Integr_Null_String){
                ConvertMonth=IntMonth;
            }else{
                ConvertMonth=null;
            }
            
            if(year!=Null_String){
                ConvertYear=integer.valueof(year);
            }else{
                ConvertYear=Integr_Null_String;
            }
            
            if(ConvertYear!=Integr_Null_String && q.Quater__c!=null){
                Startday= Date.newInstance(ConvertYear, ConvertMonth, day);
                q.End_Date__c =Startday+90;
            }else if(ConvertYear==Integr_Null_String || q.Quater__c==null){
                Startday=null;
                q.End_Date__c=null;
            }else {
                Startday=null;
                q.End_Date__c=null;
            }
            q.Start_Date__c=Startday;
            
        }
    }
    
    public static void dupRegionalError (List<Quota__c> newTrigQuo) {
        assignedtoSet =new Set<Id>();
        QuaterString =new Set<String>();
        YearString =new Set<String>();
        
        for(Quota__c q: newTrigQuo){
            if(q.Assigned_To__c!=null){
                assignedtoSet.add(q.Assigned_To__c);
            }
            
            if(q.Quater__c!=null){
                QuaterString.add(q.Quater__c);
            }
            if(q.Quater_Year__c!=null){
                YearString.add(q.Quater_Year__c);
            }
        }
        Id recordTypeId = Schema.SObjectType.Quota__c.getRecordTypeInfosByName().get('Field Sales Team Manager').getRecordTypeId(); 
        
        //List of all Quota Records with Recordtpe inside sales target currently in the database    
        List <Quota__c> allQuotaList = [SELECT ID, Name, Assigned_To__c,Quater_Year__c, RecordTypeId, Quater__c 
                                        FROM Quota__c where RecordTypeId=:recordTypeId AND Assigned_To__c IN:assignedtoSet
                                        AND Quater__c IN: QuaterString AND Quater_Year__c IN: YearString];
        //Iterate through new potential records
        if(allQuotaList!=null){
            for (Quota__c q : newTrigQuo) {
                //Needed to iterate through all existing records which is contained in the allCon List
                for (Integer i=0; i<allQuotaList.size(); i++){
                    q.addError('Duplicate Quota Detected! This record with same user and Same Quarter already exists.');
                }
            } 
        }
    }
    
    public static void Quotadupuser (List <Quota__c> dupTrigQuo){
        // Set to store email ids
        Set <String> Userset = new Set<String>(); 
        // Iterate through each Quota and add their Assigned to field to their respective Sets
        for (Quota__c Quo:dupTrigQuo) {
            Userset.add(Quo.Assigned_To__c);
        }
        // New list to store the found Assigned to field
        List <Quota__c> QuotaList = new List<Quota__c>();
        // Populating the list using SOQL
        QuotaList = [SELECT Assigned_To__c FROM Quota__c WHERE Assigned_To__c IN :Userset];
        // Iterating through each Quota record to see if the Assigned to was found
        for (Quota__c Quo:dupTrigQuo) {
            If (QuotaList.size() > 0) {
                // Displaying the error
                Quo.Assigned_To__c.adderror( 'Duplicate Assigned User Found. Please Assign a Different User.' );
            }
        }
    }   
}

My Trigger
 
trigger QuotaTrigger on Quota__c (before insert, before update,after insert,after update,before delete) {
    
    public List<Opportunity> userOppList = new List<Opportunity>(); 
    Map<Date,Date> startEndDate = new Map<Date,Date>();
    Set<Id> quotaUserIds = new Set<Id>();
    Map<Id,Quota__c> quotaMap = new Map<Id,Quota__c>();
    Map<String,Quota__c> afterquotaMap = new Map<String,Quota__c>();
    set<Id> assignedToIds = new Set<Id>();
    List<OpportunitySplit>  osList = new List<OpportunitySplit>();
    public Id fsRecordTypeId = Schema.SObjectType.Quota__c.getRecordTypeInfosByDeveloperName().get('Individual_Sales_Rep_Field_Sales').getRecordTypeId();
    public Id directorRecordTypeId = Schema.SObjectType.Quota__c.getRecordTypeInfosByDeveloperName().get('Regional_Head_Director_Sales_manager').getRecordTypeId();//9 March Maanas
    public Map<Id,Quota__c> assignedToQuotaMap = new Map<Id,Quota__c>();
    public Map<Id,Quota__c> directorQuotaMap = new Map<Id,Quota__c>();
    public Map<Id,List<Quota__c>> managerReporteeQuotaMap = new Map<Id,List<Quota__c>>();
    public List<Quota__c> quotaToUpdateManager = new List<Quota__c>();
    public Map<Quota__c,Id> reporteeManagerMap = new Map<Quota__c,Id>();
    public Map<Id,String> keyMap1 = new Map<Id,string>();
    Map<String,String> QauterDateMapping = new Map<String,String>{'Q1'=>'01/01/-03/31/','Q2' => '04/01/-06/30/',
        'Q3' => '07/01/-09/30/','Q4'=>'10/01/-12/31/'};
            
            String message = 'Error! Duplicate Quota Record For Individual Sales Rep / Field Sales (FS)';
    Set<String> newKeys = new Set<String>();   

    if(Trigger.IsInsert && Trigger.IsBefore){
        for(Quota__c qObj : Trigger.new) {
            //if(qObj.Assigned_To__c!=null){
            //QuotaHelper.getConvertedLeadCount_onQuota(Trigger.New, null);                 
            //}
            if(qObj.RecordTypeId == fsRecordTypeId || qObj.recordTypeId == directorRecordTypeId){
                String key = qObj.Assigned_To__c + qObj.Quater__c + qObj.Quater_Year__c;
                if (newKeys.contains(key)) qObj.addError(message);
                else newKeys.add(key);
            }
        }
        
        Set<String> existingKeys = new Set<String>();
        for (Quota__c q : [select Unique_Identifier_Per_Quater_Per_User__c from Quota__c WHERE Unique_Identifier_Per_Quater_Per_User__c IN :newKeys]) {
            existingKeys.add(q.Unique_Identifier_Per_Quater_Per_User__c);
        }
        
        for(Quota__c qqObj : Trigger.new) {
            if(qqObj.RecordTypeId == fsRecordTypeId){
                String key = qqObj.Assigned_To__c + qqObj.Quater__c + qqObj.Quater_Year__c;
                if (existingKeys.contains(key)) qqObj.addError(message);
            }
        } 
    }
    
    
    
    
    if(Trigger.IsInsert && Trigger.IsBefore){
        for(Quota__c singleQuota: Trigger.New){
            
            if(singleQuota.recordTypeId == fsRecordTypeId || singleQuota.recordTypeId == directorRecordTypeId){
                quotaUserIds.add(singleQuota.Assigned_To__c);
                quotaMap.put(singleQuota.Assigned_To__c,singleQuota);
                String quater =QauterDateMapping.get(singleQuota.Quater__c);
                List<String> QuaterDates = quater.split('-');
                String StartDate = QuaterDates[0];
                String EndDate = QuaterDates[1];
                String Year =singleQuota.Quater_Year__c;
                singleQuota.Start_Date__c = date.parse(StartDate+Year);
                singleQuota.End_Date__c =  date.parse(EndDate+Year);
                startEndDate.put(singleQuota.Start_Date__c,singleQuota.End_Date__c);  
                
                singleQuota.Name = singleQuota.Assigned_To_Owner__c + ' ' + singleQuota.Quater__c + ' ' + singleQuota.Quater_Year__c; 
                if(singleQuota.Unique_Identifier_Per_Quater_Per_User__c =='' || singleQuota.Unique_Identifier_Per_Quater_Per_User__c ==null)
                    singleQuota.Unique_Identifier_Per_Quater_Per_User__c = singleQuota.Assigned_To__c+singleQuota.Quater__c+singleQuota.Quater_Year__c;
                
            }
            
            if(singleQuota.recordTypeId == directorRecordTypeId){
                assignedToQuotaMap.put(singleQuota.Assigned_To__c,singleQuota);
            }    
            
        }   if(NULL !=startEndDate && startEndDate.size() > 0 ){
            for(OpportunitySplit os: [Select Id,Opportunity.Amount,SplitAmount,SplitOwnerId,Quota__c  FROM OpportunitySplit WHERE
                                      splitOwnerId IN :quotaUserIds AND
                                      Opportunity.CloseDate>= :startEndDate.keyset() AND Opportunity.CloseDate <= :startEndDate.values() 
                                      AND (Opportunity.StageName IN ('Live Won','Agreement Signed') AND Opportunity.Billing_To_Be_Initiated__c ='Yes')]){
                                          
                                          if(quotaMap.containskey(os.splitOwnerId)){
                                              system.debug(os.SplitAmount);
                                              system.debug(quotaMap.get(os.splitOwnerId).Inside_Sales_Roll_Up__c);
                                              if(quotaMap.get(os.splitOwnerId).Inside_Sales_Roll_Up__c == null){
                                                  quotaMap.get(os.splitOwnerId).Inside_Sales_Roll_Up__c = 0;
                                              }
                                              quotaMap.get(os.splitOwnerId).Inside_Sales_Roll_Up__c = quotaMap.get(os.splitOwnerId).Inside_Sales_Roll_Up__c  + (os.SplitAmount !=null ? os.SplitAmount: 0);
                                          }
                                          
                                          
                                      }
            
        }
        
        Map<String,String> tmpMap =new Map<String,String>();
        Map<String,String> reporteeManagerIds = new Map<String,String>();
        if(quotaUserIds != NULL && quotaUserIds.size() >0){
            for(User usrid : [Select Id,ManagerId from User WHERE Id IN:quotaUserIds]){
                if(usrid.ManagerId != null)
                    reporteeManagerIds.put(usrid.Id,usrid.ManagerId);
            }
            
            for(Quota__c qc : [SELECT Id,Quater__c,Quater_Year__c,Assigned_to__r.ManagerId FROM Quota__c WHERE Assigned_to__c IN : reporteeManagerIds.values()]){
                for(Id qId : quotaUserIds){
                    tmpMap.put(qId+qc.Quater__c+qc.Quater_Year__c,qc.Id);    
                }
                
            }
        }
        system.debug('tmpMap::'+ tmpMap);
        if(tmpMap != null && tmpMap.size() > 0){
            for(Quota__c q: trigger.new){
                if(tmpMap.containskey(q.Assigned_To__c + q.Quater__c + q.Quater_Year__c)){
                    q.Manager_Quota__c = tmpMap.get(q.Assigned_To__c + q.Quater__c + q.Quater_Year__c);
                    system.debug(q.Manager_Quota__c);
                }
            }
        }
        
      
        
        if(NULL != assignedToQuotaMap && assignedToQuotaMap.size() >0){
            for (aggregateResult result: [Select Assigned_To__r.managerid,quater__c,quater_year__c, 
                                          Sum(Inside_Sales_Roll_Up__c) FROM Quota__c WHERE Assigned_To__r.ManagerID IN:assignedToQuotaMap.keyset() /* AND 
Assigned_To__r.Quotas_role__c='Individual Sales Rep / Field Sales (FS)'*/ Group BY assigned_To__r.ManagerID,quater__c,quater_year__c]) {
    system.debug(result);
    if(assignedToQuotaMap.containskey((Id)result.get('ManagerId')) &&
       assignedToQuotaMap.get((Id)result.get('ManagerId')).Quater__c == result.get('Quater__c') &&
       assignedToQuotaMap.get((Id)result.get('ManagerId')).Quater_Year__c == result.get('Quater_Year__c')                                
      ){
          assignedToQuotaMap.get((Id)result.get('ManagerId')).Actual_Amount__c = (Decimal)result.get('expr0');
      }                           
    
}
        }
    }
    
    
    if(Trigger.IsInsert && Trigger.IsAfter){
        
        for(Quota__c q: trigger.new){
            if(q.RecordTypeId == fsRecordTypeId){
                afterquotaMap.put(String.ValueOf(q.assigned_to__c) + q.Quater__c+'-' + q.Quater_Year__c,q);
                assignedToIds.add(q.Assigned_To__c);
            }
            
            if(q.RecordTypeId == directorRecordTypeId){
                directorQuotaMap.put(q.Assigned_to__c,q);
            }
            
        }
        for(OpportunitySplit os: [SELECT Id,QuaterYear__c ,SplitOWnerId FROM opportunitySplit WHERE SplitOWnerId IN:assignedToIds]){
            if(afterquotaMap.containskey(String.ValueOf(os.SplitOwnerId)+ os.QuaterYear__c)){
                os.Quota__c = afterquotaMap.get(os.SplitOwnerId+ os.QuaterYear__c).Id;
                osList.add(os);
            }
        }
        
        if(NULL != osList && osList.size() > 0){
            update osList;
        }
        
        if(NULL != directorQuotaMap && directorQuotaMap.size() >0){
            system.debug('directorQuotaMap'+directorQuotaMap.size());
            
            for(Quota__c childQuota :[SELECT Id,Quater__c,Quater_Year__c,Assigned_to__c,Assigned_To__r.ManagerID, Manager_Quota__c FROM Quota__c WHERE Assigned_To__r.ManagerID IN:directorQuotaMap.keyset() AND RecordTypeId=:fsRecordTypeId ]){
                if(directorQuotaMap.containskey(childQuota.Assigned_To__r.ManagerID)){
                    if( childQuota.Quater__c == directorQuotaMap.get(childQuota.Assigned_to__r.ManagerID).Quater__c && childQuota.Quater_Year__c == directorQuotaMap.get(childQuota.Assigned_to__r.ManagerID).Quater_Year__c ){
                        childQuota.Manager_Quota__c = directorQuotaMap.get(childQuota.Assigned_to__r.ManagerID).Id;
                        quotaToUpdateManager.add(childQuota);
                        
                    }   
                } 
            }
        }
        
        if(null != quotaToUpdateManager && quotaToUpdateManager.size()> 0)
            update quotaToUpdateManager;
    }    
    
    
    if(Trigger.IsUpdate && Trigger.IsBefore){
        for(Quota__c qObj : trigger.new){
            if(qObj.Quater__c != trigger.oldMap.get(qObj.Id).Quater__c || qObj.Quater_Year__c != trigger.oldMap.get(qObj.Id).Quater_Year__c)
                qObj.addError('Error: Cannot Change Quarter/Quarter Year For User Once Created. Please Create A New Quota');
        } 
    }
    
    
    if(Trigger.IsUpdate && Trigger.IsBefore){
        for(Quota__c qObj : trigger.new){
            if(qObj.Month__c != trigger.oldMap.get(qObj.Id).Month__c || qObj.Assigned_To__c != trigger.oldMap.get(qObj.Id).Assigned_To__c)
                qObj.addError('Error: Cannot Change Month and Assigned to For User Once Created and Saved. Please Create A Different Quota Record');
        }
    }
    
    
    if(trigger.isBefore && trigger.isInsert){
        QuotaTriggerHandler.dupQuotaError(Trigger.new);
    }

    
    if(Trigger.isBefore){
        if(Trigger.isInsert){
            for(Quota__c q: Trigger.New){
                QuotaTriggerHandler.UpdateDate(Trigger.new);
            }
        }
        if(Trigger.isUpdate){
            for(Quota__c q: Trigger.New){
                Quota__c qold =Trigger.OldMap.get(q.Id);
                if(q.Quater_Year__c!= qold.Quater_Year__c || q.Quater__c!= qold.Quater_Year__c){
                    QuotaTriggerHandler.UpdateDate(Trigger.new);
                }
            }
        }
    }
    if(trigger.isBefore && trigger.isInsert){
        QuotaTriggerHandler.dupRegionalError(Trigger.new);
    }
    
    if(trigger.isAfter && (trigger.isInsert || trigger.isUpdate) ){
        Set<Id> ManagerQuota = new Set<Id>();
        for(Quota__c q : trigger.new){
            if(trigger.isInsert){
                if(q.Manager_Quota__c != null ){
                    ManagerQuota.add(q.Manager_Quota__c);
                }
            }else if(Trigger.isUpdate){
                if(q.Inside_Sales_Roll_Up__c != trigger.oldMap.get(q.Id).Inside_Sales_Roll_Up__c){
                    ManagerQuota.add(q.Manager_Quota__c);
                }
            }
        }
        List<Quota__c> finalQuota = new List<Quota__c>();
        if(ManagerQuota != null && ManagerQuota.size() > 0){
            for (aggregateResult result: [Select Manager_Quota__c, 
                                          Sum(Inside_Sales_Roll_Up__c) FROM Quota__c WHERE Manager_Quota__c IN: ManagerQuota GROUP BY Manager_Quota__c]) {
                                              if(result.get('Manager_Quota__c') != null)
                                                  finalQuota.add(new Quota__c(Id=(Id)result.get('Manager_Quota__c') ,Actual_Amount__c =(Decimal)result.get('expr0')));
                                              
                                          }
        }
        
        if(finalQuota.size () >0){
            update finalQuota;
            
        }
    }
    
    if(Trigger.isDelete && Trigger.isBefore){
        Map<Id,Decimal> finalQuotaAmount = new Map<Id,Decimal>();
        for(Quota__c q: trigger.old){
            if(q.Manager_Quota__c !=NULL){
                Decimal val =0.0;
                if(finalQuotaAmount.containskey(q.Manager_Quota__c)){
                    val = finalQuotaAmount.get(q.Manager_Quota__c) + q.Inside_Sales_Roll_Up__c !=null ? q.Inside_Sales_Roll_Up__c: 0;
                    finalQuotaAmount.put(q.Manager_Quota__c,val);
                }
                else{
                    finalQuotaAmount.put(q.Manager_Quota__c,q.Inside_Sales_Roll_Up__c != null? q.Inside_Sales_Roll_Up__c: 0 );
                }
            }
        }
        
        system.debug('finalQuotaAmount::'+ finalQuotaAmount);
        List<Quota__c> final1Quota = new List<Quota__c>();
        if(finalQuotaAmount != null && finalQuotaAmount.size() > 0){
            for (aggregateResult result: [Select Manager_Quota__c, 
                                          Sum(Inside_Sales_Roll_Up__c) FROM Quota__c WHERE Manager_Quota__c IN: finalQuotaAmount.keyset() GROUP BY Manager_Quota__c]) {
                                              
                                              final1Quota.add(new Quota__c(Id=(Id)result.get('Manager_Quota__c'),Actual_Amount__c =(Decimal)result.get('expr0') - finalQuotaAmount.get((Id)result.get('Manager_Quota__c'))));
                                          }
        }
        
        if(final1Quota.size () >0){
            update final1Quota;
            
        }
        
        
    }
}

My Test Class
 
@isTest
public class QuotaTriggerHandlerTest {
    
    public static String user(){
        User u = [Select id,name from user where isactive=true Limit 1];
        System.debug('u' + u.id);
        return u.id;
    }
    
    
    static testmethod void Duplication(){
        
        List<Quota__c> Quotas = new list <Quota__c>();
        Id RecordTypeIdQuota = schema.SObjectType.Quota__c.getRecordTypeInfosByName().get('IS Executive').getRecordTypeId(); 
        
        // insert First Quota  
        Quota__c Quo = new Quota__c(); 
        Quo.Name='Julian';
        Quo.Assigned_To__c = user();
        Quo.Month__c='March';  
        Quo.Quater_Year__c = '2020';
        Quo.Unique_Identifier_Per_Quater_Per_User__c = 'Hello';
        Quo.recordtypeid=RecordTypeIdQuota;
        Quotas.add(Quo);
        insert Quo;
        System.debug(' First Quota  inserted...and Id is .....'+Quo.Id);
         
        
        if(Quo.id!=NULL){
            Quota__c Quo1=new Quota__c();
            Quo1.Name='Testing';
            Quo1.Assigned_To__c = user();
            Quo1.Unique_Identifier_Per_Quater_Per_User__c = 'Second Quota inserted';
            Quo1.Month__c = 'march';
            Quo1.Quater_Year__c = '2020';
            Quo1.recordtypeid=RecordTypeIdQuota;
            System.debug(' Scecond Quote  inserted...and Id is .....'+Quo1.Id);
            try{
                insert Quo1;
            }
            catch(Exception ex){
                Boolean expectedExceptionThrown =  ex.getMessage().contains('Duplicate Quota Error! This record already exists.') ? true : false;
                System.debug('Error message is '+ex.getMessage());
            }
            
        }
        
    }
    static testmethod void Quateryear(){
        List<Quota__c> Quotas = new list <Quota__c>();
        
        Id RecordTypeIdQuota1 = schema.SObjectType.Quota__c.getRecordTypeInfosByName().get('Field Sales Team Manager').getRecordTypeId();
        Quota__c qt = new Quota__c();
        qt.Name = 'test';
        qt.Quater_Year__c = '2020';
        qt.Quater__c = 'Q1';
        qt.Assigned_To__c = user();
        qt.Unique_Identifier_Per_Quater_Per_User__c = 'Third Quota inserted';
        qt.RecordTypeId = RecordTypeIdQuota1;
        Quotas.add(qt);
        
        Quota__c qt1 = new Quota__c();
        qt1.Name = 'test';
        qt1.Quater_Year__c = '2020';
        qt1.Quater__c = 'Q2';
        qt1.Assigned_To__c = user();
        qt1.Unique_Identifier_Per_Quater_Per_User__c = 'fourth Quota inserted';
        qt1.RecordTypeId = RecordTypeIdQuota1;
        Quotas.add(qt1);
        
        Quota__c qt2 = new Quota__c();
        qt2.Name = 'test';
        qt2.Quater_Year__c = '2020';
        qt2.Quater__c = 'Q3';
        qt2.Assigned_To__c = user();
        qt2.Unique_Identifier_Per_Quater_Per_User__c = 'fifth Quota inserted';
        qt2.RecordTypeId = RecordTypeIdQuota1;
        Quotas.add(qt2);
        
        Quota__c qt3 = new Quota__c();
        qt3.Name = 'test';
        qt3.Quater_Year__c = '2020';
        qt3.Quater__c = 'Q4';
        qt3.Unique_Identifier_Per_Quater_Per_User__c = 'Sixth Quota inserted';
        qt3.RecordTypeId = RecordTypeIdQuota1;
        Quotas.add(qt3);
        
        insert Quotas;
        
        Quota__c qtr = new Quota__c();
        qtr.Name = 'test';
        qtr.Quater_Year__c = '2020';
        qtr.Quater__c = 'Q1';
        qtr.Assigned_To__c = User();
        qtr.Unique_Identifier_Per_Quater_Per_User__c = 'seventh Quota inserted';
        qtr.RecordTypeId = RecordTypeIdQuota1; 
        
        try{
            insert qtr;
            update qtr;
        }
        catch(Exception ex){
            Boolean expectedExceptionThrown =  ex.getMessage().contains('Duplicate Quota Detected! This record with same user and Same Quarter already exists.') ? true : false;
            System.debug('Error message is '+ex.getMessage());
        }
    }    
}

​​​​​​​​​​​​​​