I have a trigger on UserChangeEvent that upon Email change event, performs certain operations with the new email address.

Upon email change, Salesforce sends a confirmation email to the user, who then has to verify the change in order for the event to fire. I have successfully manually tested this logic and it works perfect, however, I could not find a way to write a script to test this.

My script successfully creates the user but the email change update never fires (possibly because that cannot be automated)

How can I solve this problem?

public class UserEmailChangeTriggerTest {

@isTest static void updateUserTest() {
    List<User> user_list = new List<User>();
    Id profileId = [SELECT Id FROM Profile WHERE Name='Standard User'].Id;

    for(Integer i = 1; i <= 2; i++) {
        String email = UserInfo.getUserEmail().substringBefore('@') + '+test' + i + '@' + UserInfo.getUserEmail().substringAfter('@');
        user_list.add(new User(
            FirstName = 'Test',
            LastName = 'User ' + i,
            Email = email,
            Username = email + '.test',
            ProfileId = profileId,
            Alias = 'tUser' + i,
            TimeZoneSidKey = 'America/Los_Angeles',
            EmailEncodingKey = 'UTF-8',
            LocaleSidKey = 'en_US',
            LanguageLocaleKey = 'en_US'
    System.debug('Users created -');


    insert user_list;


    user_list[1].Email = UserInfo.getUserEmail().substringBefore('@') + '+test1a@' + UserInfo.getUserEmail().substringAfter('@');
    update user_list;


  • Please, mark as best answer if I solved your question. Sep 6, 2021 at 15:48

1 Answer 1


In here a snippet how can be tested:

public class UserChangeEventHelperTest {

static void makeData(){
    //In here create the users you want to change email

public static void updateEmailTest(){
    User comUser = [SELECT Id, Email FROM User WHERE .. limit 1];
    comUser.Email = '[email protected]';
    update comUser;
    comUser = [SELECT Id, Username FROM User WHERE LegacyUuId__c='testuuid' limit 1];
    system.assertEquals('[email protected]',comUser.Username);

The error in your code is the position of Test.StartTest(). As the documentation says:

Call the Test.enableChangeDataCapture() method at the beginning of your test method before performing DML operations and calling Test.getEventBus().deliver() or Test.stopTest().

More info in here: https://developer.salesforce.com/docs/atlas.en-us.change_data_capture.meta/change_data_capture/cdc_trigger_testing.htm

  • Thank you. This worked for me.
    – Vikash
    Sep 8, 2021 at 5:23
  • Thank you for this. I was running into an issue where only events from TestSetup were publishing despite having Test.enableChangeDataCapture() & Test.getEventBus.deliver() in the correct positions in the unit test. Adding those to the TestSetup and unit test resolved my issue. Oct 17, 2022 at 17:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .