+ Start a Discussion
RupBRupB 

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

Hi,

 

- Sorry to bother, but I found no answer to this wuestion either on the web, or in the dev forum, or in the Apex documentation - 

 

I have a trigger, which detects error conditions and feeds errors to the class or field.

This gives a nice result interactively, in the GUI.

But how do I write test code to catch these errors ?  Is there a System.AssertException(), or the equivalent ? 

 

trigger createOrder on Asset (after insert, after update) {[...] if (price.get(a.Product2Id) == NULL) { System.debug(' error : no active price'); a.Product2Id.addError('no active price on the Product'); }[...]}

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
sfdccoder1sfdccoder1

Hi RupB,

 

I worked out a solution for a similar case I had where addError() was used for a record and not for a field.

I  believe though it should work for your case too.

 

The idea is to:

 

a. Write code in the test class that will cause the trigger to 'addError()'.

b. Surround the above test class code with try - catch.

cAssert that an exception is thrown and that the exception message is the same one you created with 'addError()'.

 

Here's an example (partly pseudo) code from both trigger and test class:

 

Trigger: 

 

if(myRecord.MyField == some value)

myRecord.addError('My Error Message');

 

 

Test Class: 

 

try

{

MyRecord.MyField = a value that will cause an error

update MyRocord;

throw new MyException('An exception should have been thrown by the trigger but was not.'); // 1. If we get to this line it means an error was not added and the test class should throw an exception here. 2MyException class extends Exception.

}

catch(Exception e)

{

Boolean expectedExceptionThrown =  e.getMessage().contains('My Error Message')) ? true : false;

System.AssertEquals(expectedExceptionThrown, true);

 

 

Let me know if this did the work

 

 

Message Edited by sfdccoder1 on 09-01-2009 01:32 AM

All Answers

sfdccoder1sfdccoder1

Hi RupB,

 

I worked out a solution for a similar case I had where addError() was used for a record and not for a field.

I  believe though it should work for your case too.

 

The idea is to:

 

a. Write code in the test class that will cause the trigger to 'addError()'.

b. Surround the above test class code with try - catch.

cAssert that an exception is thrown and that the exception message is the same one you created with 'addError()'.

 

Here's an example (partly pseudo) code from both trigger and test class:

 

Trigger: 

 

if(myRecord.MyField == some value)

myRecord.addError('My Error Message');

 

 

Test Class: 

 

try

{

MyRecord.MyField = a value that will cause an error

update MyRocord;

throw new MyException('An exception should have been thrown by the trigger but was not.'); // 1. If we get to this line it means an error was not added and the test class should throw an exception here. 2MyException class extends Exception.

}

catch(Exception e)

{

Boolean expectedExceptionThrown =  e.getMessage().contains('My Error Message')) ? true : false;

System.AssertEquals(expectedExceptionThrown, true);

 

 

Let me know if this did the work

 

 

Message Edited by sfdccoder1 on 09-01-2009 01:32 AM
This was selected as the best answer
RupBRupB

Sounds great, I will give it a try.

 

Rup 

RupBRupB

Hi sfdccoder1, thanks for the tip which works well.

 

Here is some more precise error detection code, especially for errors on fields ; in my case, I am detecting an error on the Product2Id field of an Asset :

catch(Exception e) {

System.Assert(e.getMessage().contains('FIELD_CUSTOM_VALIDATION_EXCEPTION'));

System.Assert(e.getMessage().contains('Product2Id'));

System.Assert(e.getMessage().contains('My Error Message'));

}

 

I hope this helps.

Cheers,

Rup 

 

Message Edited by RupB on 01-09-2009 10:57 AM
vinothvinoth

Hi I need to write test class for the following trigger, but i could able to cover 65%.

 


trigger FormTypeAttachments on Attachment (before insert) {

List<Information_document_scan__c> FormType = [select id, Select_Form_Type__c from Information_document_scan__c where id =:Trigger.New[0].ParentId];
 List<Attachment> atts = Trigger.new;
 for(Attachment a:atts) {
    if(FormType[0].Select_Form_Type__c=='Signature Card'){
        a.addError('Signature Card should have only Four attachments.To go back to record,Click "Done" Button');
    }
  }
}

"a.addError" is not covering, how to Achieve more then 75%?

Ivan GrgurinaIvan Grgurina
Hi vinoth,
trigger doesn't have to have more than 75% coverage, that rule just applies to regular Apex classes.
Kris SparksKris Sparks

sfdccoder1 Thanks for the help. This works great. I combined your answer with the specifics from RupB and checked my custom message, though I didn't throw an exception in the try block: 

try {
        center.Name = 'Update Center Name';
        update center;
      } catch(Exception error) {
        System.assert(error.getMessage().contains('Update failed.'));
        System.assert(error.getMessage().contains('FIELD_CUSTOM_VALIDATION_EXCEPTION'));
        System.assert(error.getMessage().contains('You do not have permission to '
                                                  + 'change the name of a center. '
                                                  + 'Please contact IT for assistance.')
                                                );
}
Kris SparksKris Sparks
sfdccoder1 Thanks for the help. This works great. I combined your answer with the specifics from RupB and checked my custom message.

Note: I didn't understand at first why the exception in the try block is neccesary, but it is. When I excluded it the record could update successfully and the test would "pass". The exceptions throws an error if the record updates successfully, which in my test case it should not:
private class TestAfterCenterTrigger {
  private class CenterNameUpdateException extends Exception {}

​.
.
.

try {
        center.Name = 'Update Center Name';
        update center;
        throw new CenterNameUpdateException('afterCenter.trigger should have run .addError '
                                          + 'on center.Name and invalidate the update. '
                                          + 'test must be runAs user not in authorizedProfiles');
      } catch(Exception error) {
        System.Assert(error.getMessage().contains('Update failed.'));
        System.Assert(error.getMessage().contains('FIELD_CUSTOM_VALIDATION_EXCEPTION'));
        System.Assert(error.getMessage().contains('You do not have permission to '
                                                  + 'change the name of a center. '
                                                  + 'Please contact IT for assistance.')
                                                );  
}

 
salesforce snehalksalesforce snehalk
Thanks @sfdccoder1. Your answer perfectly works for me. Just the thing is, I didn't use Custom Exception, just handled exception in catch block and checked it with the exception I am throwing from my Trigger. It worked. Thanks again..
Simon WilkinsonSimon Wilkinson
Hi, just an addendum to this.

I'd always suggest that it is better for a test to fail off the back of an assertion than an exception (I accept that in the background these things are probably defined in the same way).

In this case I would replace the new exception that is raised with an assertion that should fail if the DML operation (in this case) succeded when it shouldn't have.

Using the previous example (I've assumed this is a custom object named Center__c):
...
  try {
        center.Name = 'Update Center Name';
        update center;
        Center__c center_refreshed = [SELECT Id, Name FROM Center__c WHERE Id = :center.Id];
        System.AssertEquals('Update Center Name', center_refreshed.Name);
      } catch(Exception error) {
        ...
  }
...


 
Amit Trivedi 71Amit Trivedi 71
Technocare (https://www.lollipopguide.com/technocare-apk/)apk (https://www.lollipopguide.com/technocare-apk/) is the key application that helps to disable the frp protection on samsung devices.
Factory Reset Protection or FRP is an extra security protection for android phones so that someone cannot wipe out or factory reset your phone when it has got stolen. The FRP enabled smartphones asks to login to the google account that was previously used in the phone after the phone has been factory reset. This way it prevents the unauthorized access of a person inside your smartphone even if it has been stolen.
This can be a threat if you exchange your smartphone to someone else without disabling FRP. If in case it happens, you may have to tell the new owner your google account password to use the phone further.
surendra Kumar 132surendra Kumar 132
Technocare apk Download Google Account Manager is a Very powerful Android Apk which helps bypass FRP Technocare apk (https://appszx.com/technocare-tricks-apk/) (Google Account Bypass) and Manages Google Accounts on Android. . the App is meant to help if you ever forget the Pass. word (passcode) of the App. Technocare https://appszx.com/technocare-tricks-apk/
surendra Kumar 132surendra Kumar 132
Aponte a câmera traseira do seu telefone para o código QR na tela do seu PC. Whatsapp web (https://watsapweb.net/) Assim que o WhatsApp ler o código QR, ele conectará o telefone ao PC. Em um instante, o WhatsApp Web e o WhatsApp mobile serão sincronizados. 
Basicamente, a maneira de usar o Whatsapp Web é a mesma de quando usamos o aplicativo Whatsapp em um smartphone. No entanto, infelizmente, existem recursos do Whatsapp que não podem ser usados ​​na Web WA, a saber; chamada de vídeo e chamada de voz.
Alguns outros recursos do Whatsapp que você pode usar na web do Whatsapp incluem:
1. Conversando, o caminho é muito fácil, como usar o WA em um smartphone. Pesquise o nome de um amigo através do recurso de pesquisa WA ou aponte o cursor do computador para a última conversa com outra pessoa.
surendra Kumar 132surendra Kumar 132
WaWeb (https://wa-web.net/) Na verdade, ele não funciona sem o aplicativo para dispositivos móveis. Você precisará do seu telefone para conectar-se e usá-lo.
Adil ChaudhryAdil Chaudhry
Bypass FRP with Technocare Tricks Apk (https://top10latest.com/technocare-apk/) for Android. Download this app and install it on your device to make a new google account.
renu kumari 10renu kumari 10
Jio F90M Flash File (https://androidji.com/jio-f90m-flash-file/)
Flash file (https://www.flashfilefirmware.info/) ... Version Then You Should Flash Phone Here You Can Download Latest Flash File 7.1.1 And Update Your Phone Motorola XT2013-1 Firmware Info File Price ...
 
Muhammad Adil ChaudhryMuhammad Adil Chaudhry
Remote Gsm Edge APK Free Download (https://wifipuns.com/remote-gsmedge-apk/)