We have implemented one method using Apex for delete some records under the class which is without sharing.
In the method, first we are retrieving the list of the records(which needs to be deleted) using SOQl and if the list is not null deleting those records using DML.
We have written our code under try catch block.
In test class we are not able to cover the catch block.
Tried with multiple approaches like to cover the catch block- invalid id, null , run as particular user who does not have the access on the object. Still not able to achieve.
Below is the main class code snippet -
@AuraEnabled
public static void deleteObj(list<Id> Id) {
list<Error_Logs__c> lstErrorLogs = new list<Error_Logs__c();
try{
list< MyObject__c> items =[SELECT id
FROM MyObject__c
WHERE Id IN :Id];
if (items!=null && !items.isEmpty()){
delete items;
}
}catch (Exception e){
lstErrorLogs.add('Error'));
if(lstErrorLogs.size()>0){
insert lstErrorLogs;
}
}
}
Below is the test class code snippet -
@isTest
public static void testdeleteObj(){
List<MyObject__c> objList = [SELECT id
FROM MyObject__c];
Test.startTest();
Myclass.deleteObj(objList[0].Id);
Test.stopTest();
List<MyObject__c> queryList = [SELECT Id
FROM MyObject__c];
System.assertEquals(0, queryList.size());
}
Anyone has any idea to cover the catch in the scenario?