+ Start a Discussion
Bryan Jimenez 5Bryan Jimenez 5 

Having trouble with dynamic apex class

Hi Everyone,

I am currently trying to get a test to work for a simple dynamic apex query.

However, I am getting nothing back when running the test.

Could someone please offer some assistance?

Below is my controller
 
public class UserInfo { 
    Public List<User> getUserInfo() {
       List<User> UserInfo=[Select Name,Kolbe_Score__c,Verb__c,Question_1__c,Question_1_Answer__c,Question_2__c,Question_2_Answer__c,Question_3__c,Question_3_Answer__c,Question_4__c,Question_4_Answer__c,Question_5__c,Question_5_Answer__c,Question_6__c,Question_6_Answer__c,User_Bio_Image_ID__c
                                                      From User                                                     
                                                      Where id = :ApexPages.currentPage().getParameters().get('sfdc.userId') ];
                                                    
                                                                                          
        return UserInfo;
  }
}

My Test
@isTest
public class UserInfoTest 
{ 
    public static testMethod void TestMethod1() 
    {
        User U = new User();
        U.Kolbe_Score__c= 'Test';
        insert U;
        
        
        Test.StartTest(); 

        PageReference pageRef = Page.UserInfoVF; 
        pageRef.getParameters().put('id', String.valueOf(U.Id));
        Test.setCurrentPage(pageRef);

        UserInfo testAccPlan = new UserInfo();
  
        //testAccPlan.save(); call all your function here
        Test.StopTest();
        
    }
   
}


My Visualforce Page is called UserInfoVF

Thank you in advance for your help!
Best Answer chosen by Bryan Jimenez 5
Nayana KNayana K
@isTest
public class UserInfoTest 
{ 
    public static testMethod void TestMethod1() 
    {
		Profile p = [SELECT Id FROM Profile WHERE Name='System Administrator' LIMIT 1]; 
		User U = new User(Alias = 'standt', Email='tst@testorg.com', 
            EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
            LocaleSidKey='en_US', ProfileId = p.Id, 
            TimeZoneSidKey='America/Los_Angeles', UserName='tst@testorg.com',
			Kolbe_Score__c= 'Test');
        
        insert U;
        
        
        Test.StartTest(); 

        PageReference pageRef = Page.UserInfoVF; 
        pageRef.getParameters().put('sfdc.userId', String.valueOf(U.Id));
        Test.setCurrentPage(pageRef);

        UserInfo testAccPlan = new UserInfo();
		List<User> lstUserVerify = testAccPlan.getUserInfo();
		system.assertEquals(1, lstUserVerify.size());
        //testAccPlan.save(); call all your function here
        Test.StopTest();
        
    }
   
}

 

All Answers

Nayana KNayana K
@isTest
public class UserInfoTest 
{ 
    public static testMethod void TestMethod1() 
    {
		Profile p = [SELECT Id FROM Profile WHERE Name='System Administrator' LIMIT 1]; 
		User U = new User(Alias = 'standt', Email='tst@testorg.com', 
            EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
            LocaleSidKey='en_US', ProfileId = p.Id, 
            TimeZoneSidKey='America/Los_Angeles', UserName='tst@testorg.com',
			Kolbe_Score__c= 'Test');
        
        insert U;
        
        
        Test.StartTest(); 

        PageReference pageRef = Page.UserInfoVF; 
        pageRef.getParameters().put('sfdc.userId', String.valueOf(U.Id));
        Test.setCurrentPage(pageRef);

        UserInfo testAccPlan = new UserInfo();
		List<User> lstUserVerify = testAccPlan.getUserInfo();
		system.assertEquals(1, lstUserVerify.size());
        //testAccPlan.save(); call all your function here
        Test.StopTest();
        
    }
   
}

 
This was selected as the best answer
Bryan Jimenez 5Bryan Jimenez 5
This worked perfectly, thank you so much for your help Nayana