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
Eric Blaxton 11Eric Blaxton 11 

Help with Apex test class for user object

Hi and thanks in advance,

Help with a test class.  Not sure what I am doing yet.  How do I get code coverage on this class.  
  1. Create users to test on.  u1 should be updated.  u2 should not be

Apex Class:
public class updateAdminEmailsonRefresh
{
    public static String getEmailAddress()
    { 
        // Instantiate List        
        List<User> emailAddress = new List<User>();
        
        // Get records to work with
        for(User email: [Select Id, Email, Name From User WHERE profileId = 'xxx' AND isActive = True AND email LIKE '%.invalid' ])
            {   
                String newEmail = String.valueOf(email.email);
                emailAddress.add(email);
                newEmail = newEmail.substring(0, newEmail.length()-8); //remove '.invalid'
                email.Email = newEmail;
            }        
               // update the email address
                update emailAddress;
                
        return null;
    }       
}

Test Class:
 
/*
Created by: Eric Blaxton

This test class covers the code coverage of following classes:
1. updateAdminEmailsonRefresh
*/
@IsTest (SeeAllData=false)
public class updateAdminEmailsonRefreshTest
{
 @IsTest   static  void testUsers()
 {
     //Get Profile id of Admin
     
     Profile p = [SELECT Id FROM Profile WHERE Name='System Administrator'];
     
     List<User> emailAddress = new List<User>();
     
     
     // User email needs to be updated
     User u1 = new User(FirstName = 'Abel', LastName = 'Axiom', Alias = 'aaxio',Email = 'abel.axiom@sunoco.com.invalid',Username = 'abel.axiom@sunoco.com',CommunityNickname = 'aaxio', isActive = True,ProfileId = p.Id,
                    UserRoleId = '00Ed0000000S2ZS', ExternalSysID__c = 'abel.axiom@sunoco.com',Corporate_Office__c = 'DAL',EmailEncodingKey='UTF-8',
                    LanguageLocaleKey='en_US', TimeZoneSidKey='America/Chicago' ); 
     insert u1;
     
     User u2 = new User(FirstName = 'Bob', LastName = 'Builder', Alias = 'bbuil',Email = 'bob.builder@sunoco.com.invalid',Username = 'bob.builder@sunoco.com',CommunityNickname = 'bbuil', isActive = False,ProfileId = p.Id,
                    UserRoleId = '00Ed0000000S2ZS', ExternalSysID__c = 'bob.builder@sunoco.com',Corporate_Office__c = 'DAL',EmailEncodingKey='UTF-8',
                    LanguageLocaleKey='en_US', TimeZoneSidKey='America/Chicago' ); 
     insert u2;
                  
// In my mind, I create the users and i want to call the Apex class to run. What am I missing.  

     updateAdminEmailsonRefresh.getEmailAddress(); 
     
 }
}

Regards,
Eric
 
Best Answer chosen by Eric Blaxton 11
Eric Blaxton 11Eric Blaxton 11
Figured out my own issue.

There was another class dependent on an existing account.  I created the account and got 100% code coverage.

Note: I was going to dynamically create the account first and then create the user, but got an error.  This is an entirely different challenge.

System.DmlException: Insert failed. First exception on row 0; first error: MIXED_DML_OPERATION, DML operation on setup object is not permitted after you have updated a non-setup object (or vice versa): User, original object: Account: []

Eric