You need to sign in to do that
Don't have an account?
Dru 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:
This is the test class:
Any help as to what I'm doing wrong would be greatly appreciated
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
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,
Hi Sai,
I removed the line System.assert(!result.isSuccess()); and have now got this working.
Many thanks for your offer to help
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.
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