0

I have an Apex class defined like this:

public class HeddyLoginController {

     public HeddySession__c ps { get; set; }
     public String be  { get; set; }    

     public HeddyLoginController() {
     be = HeddyConfigurationModel.baseEndpoint;

     List<HeddySession__c> HeddySessions = [SELECT APIAuthorizationKey__c, HeddyPassword__c, HeddyUsername__c FROM HeddySession__c ORDER BY CreatedDate DESC];

     if (HeddySessions == null) {
        ps = new HeddySession__c(); 
     } else if (HeddySessions.size() == 0) {
         ps = new HeddySession__c(); 
     } else {
         ps = HeddySessions[0];
     }
}

I want to test in a test method that starts like this:

@isTest
static  void testFetch(){
    // Generate test model
    HeddyLoginController plc = new HeddyLoginController();

    HeddySession__c ps = new HeddySession__c();
    pls.ps = ps; 

But I get this error:

Variable does not exist: pls.ps

How am I suppose to declare this? How do I set this variable?

Or is this a problem because I'm instantiating a variable inside a static method? But I think all test methods need to be static? Was I suppose to write all of my code as static code?

1
  • 1
    By the way, the result of a query is never null. You can just check to see if the list is empty.
    – sfdcfox
    Nov 24, 2016 at 2:59

2 Answers 2

1

For purposes of your unit test, I actually recommend that you simply change the order of your code so the record is created first--your class will query the value without you needing to explicitly set it.

@isTest
static  void testFetch(){
    // Generate test model
    HeddySession__c ps = new HeddySession__c();
    insert ps;
    HeddyLoginController plc = new HeddyLoginController();
    System.assertEquals(ps.Id, plc.Id);

To unit test the other path (where there is no record), simply do not create the new record first:

@isTest
static  void testFetch(){
    // Generate test model
    HeddyLoginController plc = new HeddyLoginController();
    System.assertEquals(null, plc.ps.Id);

You shouldn't need to assign a value directly to ps in your unit test, because your constructor should be working.

2

Almost there, it's plc.ps = ps; not pls

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .