You need to sign in to do that
Don't have an account?
Shrey Tyagi
Apex Coverage for Database.Save exception- 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?
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?
Hi Shrey,
Can you try 1 thing in your test class
try{
Update ProRiskRev;
catch(exception ex){}
I believe that should prevent the test method from failing and also increase your coverage.
Thanks
Vivian
For example - while creating a account you assign sales_Terrorory= sa.id . Later in the code just use delete sa; and you will see that Database.error triggered . -with errolist- entity is deleted