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
SkyBlue2007SkyBlue2007 

How to test some private member variables in a class !

Hi all,

 

I tried to test my custom controller, and I got a error message "Variable is not visible" when I saved my test class.

 

Here's a controller.

public class SampleController {
   private User user {get; set;}
   private String pritest {get; set;}
   public String pubtest {get; set;} 
    
   public LoginController () {
      String userid = UserInfo.getUserId();
      user = [select testfield__c from User where id = :userid];
      pritest = 'abc';     

  pubtest = 'xyz'; } public PageReference login() { ・・・・・・・ } }

 

Here's a test class.

@isTest
private class SampleControllerTests {
   public static testMethod void testMyController() {
      Profile p = [select id from profile where name='Standard User'];
      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');   
      
      System.runAs(u) {
         PageReference pageRef = Page.SamplePage;
         Test.setCurrentPage(pageRef);

         SampleController controller = new SampleController();

         String pritest = controller.pritest;
         String pubtest = controller.pubtest;
         System.assertEquals('abc', pritest);
         System.assertEquals('xyz', pubtest);
      }
   }
}

Please let me know how to test some private member variables in a class.  

 

 Thanks,

SkyBlue2007

Best Answer chosen by Admin (Salesforce Developers) 
Shashikant SharmaShashikant Sharma

You can not assert private number varibles in test class. If you want to be assure of tehm them use system.assert in your controller itself.

All Answers

Shashikant SharmaShashikant Sharma

You can not assert private number varibles in test class. If you want to be assure of tehm them use system.assert in your controller itself.

This was selected as the best answer
SkyBlue2007SkyBlue2007

Hi Shashikant,

 

Thank you for your prompt reply.

I understand wha you said.

 

Thanks,

SkyBlue2007

Shashikant SharmaShashikant Sharma

Your welcome, it was my pleasure to solve your problem..

SF1DevSF1Dev
Now it's possible to configure private member variables and methods to be accessible from within your test class like so:
 
@TestVisible private Integer myPrivateInteger;