classtriggerssalesforceapex-codesalesforce-chatter

Apex - Test class adding Chatter Free users


I'm a newbie trying to build a test class for a registration handler that allows new chatter free users to be created for an org. To create the chatter free user, the working trigger checks a new user's email domain against a predefined list of acceptable email domains that are stored in a custom object named DomainList.

Looking for suggestions on how to better structure a test class for code coverage. Any ideas appreciated, thanks!

global class RegHandlerDomain implements Auth.RegistrationHandler {
    global User createUser(Id portalId, Auth.UserData data) {

        String domain = data.email.split('@')[1];

        List<DomainList__c> listedDomains = [SELECT id,
                                                    name
                                             FROM DomainList__c
                                             WHERE Email_Domain__c = : domain];

        if (listedDomains.size() == 1)   {
            User u = new User();
            Profile p = [SELECT Id 
                         FROM profile WHERE name = 'Chatter Free User'];

            // Use incoming email for username
            u.username = data.email;
            u.email = data.email;
            u.lastName = data.lastName;
            u.firstName = data.firstName;
            u.alias = (data.username != null) ? data.username : data.identifier;
            if (u.alias.length() > 8) {
                u.alias = u.alias.substring(0, 8);
            }
            u.languagelocalekey = UserInfo.getLocale();
            u.localesidkey = UserInfo.getLocale();
            u.emailEncodingKey = 'UTF-8';
            u.timeZoneSidKey = 'America/Los_Angeles';
            u.profileId = p.Id;

            System.debug('Returning new user record for ' + data.username);

            return u;

        } else return null;

    }

    global void updateUser(Id userId, Id portalId, Auth.UserData data) {
        User u = new User(id = userId);

        u.email = data.email;
        u.lastName = data.lastName;
        u.firstName = data.firstName;

        System.debug('Updating user record for ' + data.username);

        update(u);
    }
}

Test class is looking for a '}' before the insert

@isTest(SeeAllData = true)
public class RegHandlerTest {

    public void myTestMethod1() {
        //CREATE CHATTER FREE TEST USER
        Profile p = [select id 
                     from profile where name = 'Chatter Free User'];
        User u1 = new User(alias = 'chfree01',
                           email = 'chatterfreeuser101@testorg.com',
                           emailencodingkey = 'UTF-8',
                           lastname = 'Testing',
                           companyname = 'testorg',
                           languagelocalekey = 'en_US',
                           localesidkey = 'en_US',
                           profileId = p.Id,
                           timezonesidkey = 'America/Los_Angeles',
                           username = 'chatterfreeuser101@testorg.com');
        insert u1;
    }

    //CHECK NEW USER EMAIL DOMAIN TO SEE IF IN CUSTOM OBJECT DOMAINLIST
    List<DomainList__c> listedDomains = [select id, email_domain__c
                                         from DomainList__c 
                                         where name = 'testorg' LIMIT 1];
    if (listedDomains.size() == 1) System.debug('WORKS');

}

Solution

  • Fir of all you need create a proper unit test, because your is not correct.

    @isTest(SeeAllData = true)
    public class RegHandlerTest {
    
        @isTest private static void verifyInsertChatterFreeUser() {
            //CREATE CHATTER FREE TEST USER
            Profile p = [SELECT id 
                         FROM profile 
                         WHERE name = 'Chatter Free User'];
            User u1 = new User(alias = 'chfree01',
                               email = 'chatterfreeuser101@testorg.com',
                               emailencodingkey = 'UTF-8',
                               lastname = 'Testing',
                               companyname = 'testorg',
                               languagelocalekey = 'en_US',
                               localesidkey = 'en_US',
                               profileId = p.Id,
                               timezonesidkey = 'America/Los_Angeles',
                               username = 'chatterfreeuser101@testorg.com');
            insert u1;
            // perform some assertions regarding expected state of user after insert
    
            //CHECK NEW USER EMAIL DOMAIN TO SEE IF IN CUSTOM OBJECT DOMAINLIST
            List<DomainList__c> listedDomains = [SELECT id, email_domain__c
                                                 FROM DomainList__c 
                                                 WHERE name = 'testorg' LIMIT 1];
            // as you've annotated class with SeeAllData=true, this assertion always will be true
            System.assertEquals(1, listedDomains.size()); 
        }
    
        @isTest private static void verifyUpdateChatterFreeUser() {
            //CREATE CHATTER FREE TEST USER
            Profile p = [SELECT id 
                         FROM profile 
                         WHERE name = 'Chatter Free User'];
            User u1 = new User(alias = 'chfree01',
                               email = 'chatterfreeuser101@testorg.com',
                               emailencodingkey = 'UTF-8',
                               lastname = 'Testing',
                               companyname = 'testorg',
                               languagelocalekey = 'en_US',
                               localesidkey = 'en_US',
                               profileId = p.Id,
                               timezonesidkey = 'America/Los_Angeles',
                               username = 'chatterfreeuser101@testorg.com');
            insert u1;
            // perform some assertion regarding expected state of user after insert
    
            // change something on user
            u1.email = 'test.chatter.free@gmail.com';
            update u1;
    
            // perform some assertions regarding expected state of user after update
            // System.assertEquals('expected value', u1.field);
    
    
        }
    
    }