+ Start a Discussion
shrey.tyagi88@tcs.comshrey.tyagi88@tcs.com 

Code Coverage to cover exceptions of Database.Save method-Please help.

Hi Everyone,
        I have a question on how to cover exceptions of Database.Save method. The method I want to cover is given below:

Apex Code : 
     Database.SaveResult[] lsr = Database.insert(GateReviewShrs,false); 
     // Create counter
     Integer i=0;
     // Process the save results
        for(Database.SaveResult sr : lsr){
            if(!sr.isSuccess()){
                // Get the first save result error
                Database.Error err = sr.getErrors()[0];
                if(!(err.getStatusCode() == StatusCode.FIELD_FILTER_VALIDATION_EXCEPTION  
                                               &&  err.getMessage().contains('AccessLevel'))){

                    trigger.newMap.get(GateReviewShrs[i].ParentId).
                      addError(
                       'Unable to grant sharing access due to following exception: '
                       + err.getMessage());
                }
            }
            i++;
        }
I am trying to cover this exception by making a wrong entry in my test class (one of inactive user). Code given below: 


@isTest
public class Test_PrasApexCode {
    
    static testMethod void testDoGet1() {



        
        Costpoint_Project__c testCostpointProject = new Costpoint_Project__c();
        testCostpointProject.Name = '0281300.003';

        insert testCostpointProject;

        //Fetch related Project Risk Review form.
        Project_Risk_Review__c ProRiskRev=[Select id,DVP__c,Project_Manager__c,Chair_Reviewer__c,Delegate__c 
                                           from Project_Risk_Review__c where Costpoint_Project__c=:testCostpointProject.Id];

        List<User> u2= [SELECT Id FROM User WHERE isActive=False and ProfileId=:Label.PRAS_RTI_BusinessDevelopment_Profile_Id Limit 1];

  
         ProRiskRev.Delegate__c=u2[0].Id;
         Update ProRiskRev;
      
But when I try to do this my coverage increases but test method fails . It has to fail because the user is inactive. Is there any way to pass the test method and increase the coverage?
 
shrey.tyagi88@tcs.comshrey.tyagi88@tcs.com
Hi Everyone,
        I have a question on how to cover exceptions of Database.Save method. The method I want to cover is given below:

Apex Code : 
     Database.SaveResult[] lsr = Database.insert(GateReviewShrs,false); 
     // Create counter
     Integer i=0;
     // Process the save results
        for(Database.SaveResult sr : lsr){
            if(!sr.isSuccess()){
                // Get the first save result error
                Database.Error err = sr.getErrors()[0];
                if(!(err.getStatusCode() == StatusCode.FIELD_FILTER_VALIDATION_EXCEPTION  
                                               &&  err.getMessage().contains('AccessLevel'))){

                    trigger.newMap.get(GateReviewShrs[i].ParentId).
                      addError(
                       'Unable to grant sharing access due to following exception: '
                       + err.getMessage());
                }
            }
            i++;
        }
I am trying to cover this exception by making a wrong entry in my test class (one of inactive user). Code given below: 


@isTest
public class Test_PrasApexCode {
    
    static testMethod void testDoGet1() {



        
        Costpoint_Project__c testCostpointProject = new Costpoint_Project__c();
        testCostpointProject.Name = '0281300.003';

        insert testCostpointProject;

        //Fetch related Project Risk Review form.
        Project_Risk_Review__c ProRiskRev=[Select id,DVP__c,Project_Manager__c,Chair_Reviewer__c,Delegate__c 
                                           from Project_Risk_Review__c where Costpoint_Project__c=:testCostpointProject.Id];

        List<User> u2= [SELECT Id FROM User WHERE isActive=False and ProfileId=:Label.PRAS_RTI_BusinessDevelopment_Profile_Id Limit 1];

  
         ProRiskRev.Delegate__c=u2[0].Id;
         Update ProRiskRev
;
      
But when I try to do this my coverage increases but test method fails . It has to fail because the user is inactive. Is there any way to pass the test method and increase the coverage?