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
praveen kumar 110praveen kumar 110 

I am getting in null while running test class ,I have inserted proper data also but getting null ,Please any check it bleow help me out....?


pubic class GSkController {


 public GSkController (ApexPages.StandardSetController setController)

{

  if(caseID!=null && caseId!='')
        {
            for(String strCaseId : caseId.split(','))
            {
                lstSelectedCaseIds.add(strCaseId);
            }
            if(lstSelectedCaseIds!=null && lstSelectedCaseIds.size()>0)
            {
                 lstSelectedCase = [select Id,Contact.Id,Contact.Name, Status from Case where ID in : lstSelectedCaseIds];
                                     
                                     System.debug('lstSelectedCase******'+lstSelectedCase[0].Contact.Id);

                    // "contact id is getting null"
            }


}


------------------------------------------------------------------------------------------------
Test Class:

@isTest(seeAllData=true)
private class TestGSkController {

  static testMethod void MyunitTest()
  {  
    
    Profile p = [select id, name from Profile where  name = 'System Administrator'];
     User u = new User(Alias = 'standt', Email='standarduser@testorg.com',
      EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US',
      LocaleSidKey='en_US', ProfileId = p.Id,
      TimeZoneSidKey='America/Los_Angeles', UserName='standarduser@testorg.com');

        
                
   Insert u;
       
    System.runAs(u)
    {
      
          Contact conTestContact = new contact();
      conTestContact.name='test';          
          insert conTestContact;

          System.debug('conTestContact***************'+conTestContact.id); // I am getting Id
    
        Case ObjCase=new Case();
        
           ObjCase.Name='test';       
        objCase.Contact=conTestContact;
        
    insert objcase;
        
        System.debug('objcase***********'+objcase.contact.id);  // I am getting id value  
    
       pageReference.getParameters().put('id',ObjCase.id);          
                    
                  
             GSkController ObjKey = new GSkController(new ApexPages.StandardSetController(Database.getQueryLocator([select Id,Contact.id from Case where ID =:ObjCase.id])));
            
  }
}
}

   
Neetu_BansalNeetu_Bansal
Hi Praveen,

To put parameters in page, you need to set current page with your page. See the code below, I have set the current page at line no 27, replace YourPageName with the name of the page.
@isTest(seeAllData=true)
private class TestGSkController
{
	static testMethod void MyunitTest()
	{  
		Profile p = [select id, name from Profile where  name = 'System Administrator'];
		User u = new User( Alias = 'standt', Email='standarduser@testorg.com',
							EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US',
							LocaleSidKey='en_US', ProfileId = p.Id,
							TimeZoneSidKey='America/Los_Angeles', UserName='standarduser@testorg.com' );
		insert u;
    
		system.runAs(u)
		{
			Contact conTestContact = new contact();
			conTestContact.name='test';          
			insert conTestContact;
			system.debug('conTestContact***************'+conTestContact.id); // I am getting Id
    
			Case ObjCase=new Case();
			ObjCase.Name='test';       
			objCase.Contact=conTestContact;
			insert objcase;
			System.debug('objcase***********'+objcase.contact.id);  // I am getting id value  

			// Create the instance of visualforce page and put contact id into parameters
	        Test.setCurrentPageReference(new PageReference('Page.YourPageName')); 
	        System.currentPageReference().getParameters().put('id', ObjCase.id);
			
			GSkController ObjKey = new GSkController( new ApexPages.StandardSetController( Database.getQueryLocator( [select Id,Contact.id from Case where ID 	=:ObjCase.id])));
        }
	}
}
Let me know, if you need any other help.

Thanks,
Neetu
praveen kumar 110praveen kumar 110
Hi Neetu, 

Event i have given  "Test.setCurrentPageReference(new PageReference('Page.YourPageName'));" in Test class,after Running the class ,what ever the contact id putting in test class , not getting class ...i amgettin as "Null"   ....what was the mistake i done,  dont know.
Please help me out.

Thanks,
Praveen.
Neetu_BansalNeetu_Bansal
Hi Praveen,

I think you are not setting the page name properly, let's suppose your page name is CaseView, then use the code like this:
// Create the instance of visualforce page and put case id into parameters
Test.setCurrentPageReference(new PageReference('Page.CaseView')); 
System.currentPageReference().getParameters().put('id', ObjCase.id);
Try this, it will definately resolve your problem.

Thanks,
Neetu