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
djamouhadjamouha 

Testing addError return Validation Errors While Saving Record(s)

Hi guys,
I am having a trouble with the addError method. In fact I have done a litte code that block any user of our org from sending private message using Chatter. That looks like this : 
public static void insertNewChatterMessage(List<ChatterMessage> listeChatterMessages){
    	for(ChatterMessage c : listeChatterMessages){
    		
    		System.debug('chat mess : '+c);    		
    		c.Body.addError('Private Messages are not allowed');	
    	}
}

This parts works pretty well as it returns on GUI the custom message and prevent sending the message.
The issue comes in the Test Class. 
I am just doing something as simple as that : 
@isTest(SeeAllData=true)
    static void testChatterMessageBeforeInsert(){
    		Test.startTest();
           
    		try{
                ConnectApi.ChatterMessages.sendMessage('Test chatter message', String.valueOf(userReceivingChatterMessage.Id));
                throw new MyChatterException('Private Messages are not allowed');
            }
            catch(DmlException e){
                system.debug('###Mess : '+e.getMessage());
                system.assert(e.getMessage().contains('Private Messages are not allowed'));
            }
            catch(MyChatterException f){
                system.debug('###Mess : '+f.getMessage());
                system.assert(!f.getMessage().contains('Private Messages are not allowed'));
            }

But I always get this error when running the test : "Validation Errors While Saving Record(s)"

Any idea of where I am doing bad.

Regards
bob_buzzardbob_buzzard
The validation errors message should tell you what the underlying problem is - what is that?
djamouhadjamouha
Hi Bob,
The only error revealed by the test is what I mentionned : "Validation Errors While Saving Record(s)".
And in the debug log, I just see that when the trigger is called, I have this : "Internal Salesforce.com Error".
Here are the only informations I have about the error.
djamouhadjamouha
Thrown Error by Apex Test Execution