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
Dru Rattigan 11Dru Rattigan 11 

Trigger Test Error

I have a trigger that I have created a test for, this gives me 100% coverage for the trigger but the test has an error System.AssertException: Assertion Failed
This is the trigger:
trigger AddUUID on Advertisement__c (before insert) {
    
    Uuid myUuid = new Uuid();
String myUuidValue = myUuid.getValue();
    
    for(Advertisement__c  acc:trigger.new){acc.UUID__c = myUuidValue;}

}

This is the test class:
@istest
public class TestAdvertisementUUID {
    @istest
    public static void testadvertisement(){
       Advertisement__c ADV = new Advertisement__c(Apprenticeship_Opportunity__c='0064L000009nWLaQAM');
        test.startTest();
        Database.SaveResult result = Database.insert(ADV,False);
        Test.stopTest();
        System.assert(!result.isSuccess());
    }
}

Any help as to what I'm doing wrong would be greatly appreciated
Sai PraveenSai Praveen (Salesforce Developers) 

Hi,

Can you let us know what is that Uuid and is it some class where you re generating the value. If so Can you share that particular class as well so I can share the total test class with 100% coverage.

Thanks,
 

Dru Rattigan 11Dru Rattigan 11

Hi Sai,

I removed the line  System.assert(!result.isSuccess()); and have now got this working.
 

Many thanks for your offer to help

Dru

Neha Arora 50Neha Arora 50
Hi Dru,

Plese check the value you are using in the assert method. i.e., by using System.debug(!result.isSuccess());
Because to check the result of Database.saveResult we do not use the exclamatory mark '!'. If your records are saved successfully then the result.isSuccess will return true but since you are using the 'NOT' operator or exclamatory mark '!' it will reverse your output & will return as false.
In the assert method if your output is false it will throw a fatal error.

So basically in your assert method, you should check for result.isSuccess instead of !result.isSuccess.

Let me know if this helps and mark this as the best answer.
Neha Arora 50Neha Arora 50
You can check both these links to know more about Database.SaveResult and System.assert method
https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_database_saveresult.htm
https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_system.htm#apex_System_System_assert