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
Yogesh BiyaniYogesh Biyani 

test class to prevent deletion

I am writing a trigger to prevent detetion of certain accounts 
trigger doNotDelete on Account (before delete) {
    // Prevent the deletion of accounts if it is a customer
    for (Account a : Trigger.old) {
       if(a.Customer_ID__c!=NULL)
            a.adderror('Cannot delete Active Account!');
    }
    
}
How do I write a test class to this success of the trigger? 
@isTest 
public class doNotDeleteTest {
    
    @isTest public static void
        deleteTest()
    {
        Account a =new Account(Name='Test');
        a.Customer_ID__c =1234;
        insert a;
        
        try {
            delete a;
        }
        catch (Exception e) {
   		   System.debug(e.getTypeName());
           System.debug(e.getMessage());
                                          
        }
        finally {
            // Something executed whether there was an error or not
        }
        
    }
}
Best Answer chosen by Yogesh Biyani
Joseph MinikJoseph Minik
This should give you the coverage you expect, Yogesh. See my comments throughout the code:
 
@isTest 
public class doNotDeleteTest {
    
    @isTest public static void deleteTest() {
        Account a = new Account(Name='Test');
        a.Customer_ID__c = 1234;
        insert a;
        
        // variable to store exception message type
        String exceptionType = '';
            
        // variable to store exception message content
        String exceptionMessage = '';
        
        try {
            delete a;
        }
        catch (Exception e) {
           // if there is an exception, assign its type to the new exceptionType variable
           exceptionType = e.getTypeName();
           
           // if there is an exception, assign its message to the new exceptionMessage variable
           exceptionMessage = e.getMessage();                           
        }
        
        // assert that the exception exists, and that its type and message are accurate
        System.assertEquals(exceptionType, 'System.DmlException', 'Unexpected exception type.');
        System.assertEquals(exceptionMessage.contains('Cannot delete Active Account!'), true, 'Unexpected exception message.');
        
        // validate the original Account still exists in your system
        List<Account> accountList = [SELECT Id FROM Account WHERE Name='Test' AND Customer_ID__c = '1234'];
        System.assertEquals(accountList.size(), 1, 'Unexpected Number of Accounts returned.');
    }
}

 

All Answers

Joseph MinikJoseph Minik
This should give you the coverage you expect, Yogesh. See my comments throughout the code:
 
@isTest 
public class doNotDeleteTest {
    
    @isTest public static void deleteTest() {
        Account a = new Account(Name='Test');
        a.Customer_ID__c = 1234;
        insert a;
        
        // variable to store exception message type
        String exceptionType = '';
            
        // variable to store exception message content
        String exceptionMessage = '';
        
        try {
            delete a;
        }
        catch (Exception e) {
           // if there is an exception, assign its type to the new exceptionType variable
           exceptionType = e.getTypeName();
           
           // if there is an exception, assign its message to the new exceptionMessage variable
           exceptionMessage = e.getMessage();                           
        }
        
        // assert that the exception exists, and that its type and message are accurate
        System.assertEquals(exceptionType, 'System.DmlException', 'Unexpected exception type.');
        System.assertEquals(exceptionMessage.contains('Cannot delete Active Account!'), true, 'Unexpected exception message.');
        
        // validate the original Account still exists in your system
        List<Account> accountList = [SELECT Id FROM Account WHERE Name='Test' AND Customer_ID__c = '1234'];
        System.assertEquals(accountList.size(), 1, 'Unexpected Number of Accounts returned.');
    }
}

 
This was selected as the best answer
Yogesh BiyaniYogesh Biyani
@Joseph - Thank you very much!