You need to sign in to do that
Don't have an account?

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
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
Let us know if this will help you
All Answers
Change the test class code like this:
Please mark this as the solution, if it solves your problem.
Thanks
Let us know if this will help you
Now it works!!
Cheers!
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?
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!
Regards,
Nandana