function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
randheer practiserandheer practise 

iam writing test class to DeleteDML class iam gettinh 80%coverage but iam unable to cover code in Catch block.thanks in advance

DeleteDMl class
public class DeleteDml {
    public void accountInserting(){
        //fetching old records with name kanus
        List<Account> accounts =[SELECT id,name from Account WHERE name='kanus'];
        try{
        delete accounts;
        }catch(Exception e){
            system.debug('records are not deleted');
            
        }
    }
}

testclass
@istest
public class DeleteDML_test {
    public static testMethod Account insertingAccount(){
        test.startTest();
        Account a = new Account();
        a.Name='kanus';
        insert a;
        DeleteDml d = new DeleteDml();
        d.accountInserting();
        Test.stopTest();
        return a;
       
    }
    public static testMethod void Method2(){
      Test.startTest();
        Account a1=insertingAccount();
        Account a2=[SELECT id,name FROM Account WHERE name='kanus123'];
        try{
            delete a2;
        }catch(Exception e){
            system.debug('no record with name kanus123');
        }
        
        test.stopTest();
         DeleteDml d1 = new DeleteDml();
        d1.accountInserting();
    }
}
Best Answer chosen by randheer practise
Magesh Mani YadavMagesh Mani Yadav
Hi randheer,
In the above scenario a case was linked with an account. Hence its not possible to delete an account which has a related case.
therefore the catch block was executed returning 100% coverage.
 

All Answers

manasa gundars 11manasa gundars 11
Hi randheer,

Please find the below modified test class which gives 100 percent code coverage.

@isTest
public class DeleteDML_test {
    public static testmethod void accountInserting()
    {
        //account instance
        account acc = new account();
        acc.Name = 'kanus';
        acc.BillingCity = 'hyderabad';
        insert acc;
        //case instance
        case c = new case();
        c.AccountId = acc.Id;
        c.Subject = 'call';
        insert c;
        test.startTest();
        DeleteDml DeleteAcc = new DeleteDml();
        DeleteAcc.accountInserting();
        test.stopTest();
    }
}

Best Regards,
Manasa.G
randheer practiserandheer practise
thanks manasa,can you tell me the logic  that how you cover the code of catch(Exception e){
}
randheer practiserandheer practise
can you tell me the mistake what i had done...in brief
randheer practiserandheer practise
ok ok i got it manasa thank you so much..
randheer practiserandheer practise
can anyone tell me that in the above scenario why she created case with comparing Newly created record
Magesh Mani YadavMagesh Mani Yadav
Hi randheer,
In the above scenario a case was linked with an account. Hence its not possible to delete an account which has a related case.
therefore the catch block was executed returning 100% coverage.
 
This was selected as the best answer