+ Start a Discussion
Vigneshwaran LoganathanVigneshwaran Loganathan 

Profile : Test class

Hello, 

I Have apex class which checks if profile id != null and do some acions in user. Below is the code, 
 
public void registerUser()
{
   if(//condition)
        {
             if()
             {
                //some actions
             }
             else
             {
             Database.DMLOptions dmo = new Database.DMLOptions();
             dmo.EmailHeader.triggerUserEmail = true;
             
             Profile p = [select id from profile where name = 'YMCA Community User'];
             
             if(p.id != null)
             {
             try
             { 
                newUser = new User();
                newUser.Username = mEmail;
                newUser.firstName  = acnt[0].FirstName;
                newUser.LastName = acnt[0].LastName;
                //and all user field assigning fields..

                newUser.setOptions(dmo);
                List<Account> a = [SELECT Id from Account where Email__c = :mEmail];
                String accountId = a[0].Id;
                Site.createPortalUser(newUser,accountId,passwd);

             }
             catch(Exception Ex)
             {
                ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.Error,ex.getmessage()));
                //return null;
             }
        }

I need to Execute the Try part, but below test class is not working..  
 
static testMethod void testselfregcon2(){
    
       
        Profile p= [Select id from profile where Name ='YMCA Community User']; 
        system.debug('>>profT'+p);
                
        user objuser=new user(Username='testuser@email.com', ContactId=objcon.Id,LastName ='uuser', Email='test1231231@abc.com', Alias='usr', CommunityNickname='test', TimeZoneSidKey='America/Los_Angeles', LocaleSidKey='en_US', EmailEncodingKey='UTF-8', ProfileId = p.id, LanguageLocaleKey='en_US');
        insert objuser;
       
        system.runAs(objuser)
        {
        CommunitiesSelfRegCustomController1 objcls= new CommunitiesSelfRegCustomController1();
        objcls.registerUser();
        }
    }
Guess, Profile Id is not passing to class because of which else part is not working. can anyone help on this.? 

Thanks
Vignesh
LBKLBK
You are trying to access an existing data (Profile ID of the YMCA Community User) in the test class.

You need to use "@isTest(SeeAllData=true)" annotation above your test class to access this data.