0

TL ; DR : I'm unable to fill a getter/setter list (that is filled via SOQL query) during testing.

When unit testing a controller class, a certain {get;set;} list is not getting populated (it is null).

This list is populated via a SOQL query.

public class myClass
{
    public list<Obj__c> myList {get;set;}

    public void doFill()
    {
        string queryStr = 'SELECT Id, Name, count__c, myField__c FROM Obj__c';
        if(condition)
        {
            queryStr += 'WHERE count__c =: 10';
        }
        myList = database.query(queryStr);
        system.debug(myList);
    }

    public void doForeignFill()
    {
        myList = myUtilityClass.getRecords();
        system.debug(myList);
    }

    public void myFunc()
    {
        system.debug(myList);
        if(mylist != null && myList.size() != 0)
        {
            //logic goes here
        }
    }
}

In one case, the query function is defined locally, in the other, its in a different class. Both the functions are similar (they only have different naming schemes).

However, when I write the test class, it does NOT fill myList. It comes up as null in the debug logs.

Here's what I have tried

myClass mCTest = new myClass();
list<Obj__c> mLTest = new list<Obj__c>();
Obj__c objTest = new Obj__c();

insert mLTest;
insert objTest; //yes, I know this "ought" to go into a list.

//fill up the list

mCTest.myList = mLTest;
mCTest.myList.add(objTest);

I am quite sure that I've filled up all the required fields that are requested in the SOQL query. And both the functions are working correctly on the production site, so I dont think its a problem with the class's code.

0

You need to change few things here. You can call the method or first add variable in test class list and then assign that list.

myClass mCTest = new myClass();
list<Obj__c> mLTest = new list<Obj__c>();
Obj__c objTest = new Obj__c(count__c = 10);
mLTest.add(objTest )
insert mLTest;
//insert objTest; //yes, I know this "ought" to go into a list.

//fill up the list
mCTest.doFill(); // you can fill the test using calling the method
//mCTest.myList = mLTest; //or you can directly assign this to list
mCTest.myList.add(objTest);
  • yes, but the doFill() and doForeignFill() functions return null AND directly assigning the object to the list does NOT work. I donot know how to fix this. – fraglord Oct 28 '16 at 4:58
  • @fraglord check my updated answer you need to populate Count field with 10. Also not sure where you declare the condition – Tushar Sharma Oct 28 '16 at 5:01
  • thats ok, condition is declared correctly. I only typed the most important parts of the code. – fraglord Oct 28 '16 at 5:09
  • @fraglord when we run test class from developer console then it display which part is covered which is not. As you share partial code only so I suggest to heck there and see which lines are not covered and if possible share the screenshot. – Tushar Sharma Oct 28 '16 at 5:15
  • pixhoster.info/f/2016-10/8128cd64aaf32efa8758cd7d6f860253.png this is the code. Please notice the condition checking and the external function and the fact that the first test for null list PASSES (atleast thats what I can see from the coverage) and fails at the for loop. pixhoster.info/f/2016-10/bb19513d8932d5a200cafff94ef9dfab.png this is the log of the 2 system.debug() messages. As you can see, it is null. – fraglord Oct 28 '16 at 5:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.