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
AlbertoSMAlbertoSM 

How do I test for an exception('addError') in a class?

Hi everyone,

I have to test this class:

List<Account> oldAccounts   = Trigger.old;
        for(Account check : oldAccounts){
            if(check.Account_ID__c!=null){
                check.addError('Accounts that have SAP Account ID can not be deleted');
            }
        }

I know that in theese cases one has to use try/catch methods to solve the test.
But I've been trying for hours and I don't know how to solve it.

I post what I did so far, it covers everything except last line ('addError'):

@isTest
public class TriggerAccountDeleteTest {
      public class MyException extends Exception {}
       static testMethod void metodoDeletAccount(){
        
             //Create an Account with his required fields
             Account accountPrueba = new Account();
                accountPrueba.Name = 'Siemens2';
                accountPrueba.Account_Country__c = 'a014E000005XNKh';
                accountPrueba.Language__c = 'a064E0000060Wnr'; 
                accountPrueba.Department__c = 'Services';
            
            //Insert it
            insert accountPrueba ;
           
            //Test.startTest();  
                try{
                    delete accountPrueba;
                    Account deletedAccount = [SELECT Id, IsDeleted FROM Account WHERE Id = :accountPrueba.Id ALL ROWS];
                    System.assertEquals(deletedAccount.IsDeleted, true);
        
                    }
                 catch(Exception e){
                    Boolean b = null;
                    accountPrueba2.Account_ID__c = String.valueOf(b);
                    System.assert(e.getMessage().contains('Accounts that have SAP Account ID can not be deleted'));
                 }
               Test.stopTest();   
        
    }
}
Thank you for your time
Cheers,
Alberto
Best Answer chosen by AlbertoSM
Amit Chaudhary 8Amit Chaudhary 8
Please below test class
@isTest
public class TriggerAccountDeleteTest 
{
	static testMethod void metodoDeletAccount()
	{
	
		 Account accountPrueba = new Account();
			accountPrueba.Name = 'Siemens2';
			accountPrueba.Account_Country__c = 'a014E000005XNKh';
			accountPrueba.Language__c = 'a064E0000060Wnr'; 
			accountPrueba.Department__c = 'Services';
			accountPrueba.Account_ID__c ='ID111';
		insert accountPrueba ;
	   
		Test.startTest();  
			try
			{
				delete accountPrueba;       
			}
			catch(Exception e)
			{
				Boolean expectedExceptionThrown =  e.getMessage().contains('Accounts that have SAP Account ID can not be deleted')) ? true : false;
				System.AssertEquals(expectedExceptionThrown, true);                
			}
		Test.stopTest();   
	
	}
}

Let us know if this will help you
 

All Answers

Sure@DreamSure@Dream
Hi Alberto,

Change the test class code like this:
@isTest
public class TriggerAccountDeleteTest {
      public class MyException extends Exception {}
       static testMethod void metodoDeletAccount(){
        
             //Create an Account with his required fields
             Account accountPrueba = new Account();
                accountPrueba.Name = 'Siemens2';
                accountPrueba.Account_Country__c = 'a014E000005XNKh';
                accountPrueba.Language__c = 'a064E0000060Wnr'; 
                accountPrueba.Department__c = 'Services';
            
            //Insert it
            insert accountPrueba ;
    //insert accountPrueba2 here, by assigning some value to Account_ID__c -- update
           

              Test.startTest();  
                try{
                    delete accountPrueba;
                    Account deletedAccount = [SELECT Id, IsDeleted FROM Account WHERE Id = :accountPrueba.Id ALL ROWS];
                    System.assertEquals(deletedAccount.IsDeleted, true);
                    delete accountPrueba2;
                    }
                 catch(Exception e){
                    //Boolean b = null;
                    //accountPrueba2.Account_ID__c = String.valueOf(b);
                    System.assert(e.getMessage().contains('Accounts that have SAP Account ID can not be deleted'));
                 }
               Test.stopTest();   
        
    }
}

Please mark this as the solution, if it solves your problem.

Thanks
Amit Chaudhary 8Amit Chaudhary 8
Please below test class
@isTest
public class TriggerAccountDeleteTest 
{
	static testMethod void metodoDeletAccount()
	{
	
		 Account accountPrueba = new Account();
			accountPrueba.Name = 'Siemens2';
			accountPrueba.Account_Country__c = 'a014E000005XNKh';
			accountPrueba.Language__c = 'a064E0000060Wnr'; 
			accountPrueba.Department__c = 'Services';
			accountPrueba.Account_ID__c ='ID111';
		insert accountPrueba ;
	   
		Test.startTest();  
			try
			{
				delete accountPrueba;       
			}
			catch(Exception e)
			{
				Boolean expectedExceptionThrown =  e.getMessage().contains('Accounts that have SAP Account ID can not be deleted')) ? true : false;
				System.AssertEquals(expectedExceptionThrown, true);                
			}
		Test.stopTest();   
	
	}
}

Let us know if this will help you
 
This was selected as the best answer
AlbertoSMAlbertoSM
Thank you very much for your quick response Amit Chaudhary 8.
Now it works!!
Cheers!
nandananandana
Hi...Alberto Salas Mellado,
This is not your answer. But i have a doubt.If you dont mind pls tell me. How can you post that much of leanthy code... Actually so many times i'm trying to post my doubts, But it showing maximum 255 characters only like that. can you pls tell me how to you achieve this?
AlbertoSMAlbertoSM
Hi Nandana
Simply I've just copied and pasted the code and everything was ok. In the PickList I chose Apex Code Development and that's it! 
Sorry if I did'nt solve your problem!
nandananandana
Thank you.  Based on ur information i got it.

Regards,
Nandana