Test data fields return null in class but are populated in unit test

I have a Visualforce page that is using a Controller extension. The extension’s SOQL query is not returning the data that I expect when I run my unit tests, although it works inside of our sandbox. The query returns Account and Opportunity fields but when I filter on certain Account fields I end up with 0 rows returned.

  • Account.Cohort__c is a Lookup to a custom object, Cohort__c
  • Account.Enrollment_Status__c is a picklist

This query, in my extension class, returns 0 rows because the Account fields are null:

SELECT Account.Name, Account.Enrollment_Status__c, Cohort__c
FROM Opportunity
WHERE StageName = 'Enrolled' AND
AccountId IN 
(
 SELECT Id
 FROM Account
 WHERE Cohort__c = :chosenCohort.Id AND Enrollment_Status__c IN ('Enrolled','Graduated'))

The testing class:

@isTest
private class StudentDirectoryCohortExtensionTest{
    static testMethod void StudentDirectoryOnlyReturnsEnrolledAndGraduated_Test(){
        TestRecordSet db = new TestRecordSet();        

        // Present for helping debug SOQL in extension class
        List<Account> tempAccounts = [SELECT Name, Cohort__c, Enrollment_Status__c FROM Account WHERE Cohort__c = :db.cohorts[0].Id];
        System.Debug('Test after insert');
        for (Account acc: tempAccounts){
            System.Debug('Enrollment Status: ' + acc.Enrollment_Status__c);
        }

        Test.startTest();
        PageReference pageRef = Page.StudentDirectory;
        Test.setCurrentPageReference(pageRef);

        // Call Constructor
        ApexPages.StandardController sc = new ApexPages.standardController(db.cohorts[0]);
        StudentDirectoryCohortExtension myExtension = new StudentDirectoryCohortExtension(sc);

        Test.stopTest();

        // Count the number of items in cohortMembers
        // This Assert fails; expected 4, getting 0
        integer memberCount = myExtension.cohortMembers.size();
        System.assertEquals(db.NumberOfCohortStudents , memberCount);   
    }

    // Holds test data to be used during method testing.
    private class TestRecordSet{    
        private List<Account> accounts;
        private List<Opportunity> applications;
        private List<Cohort__c> cohorts;
        private List<Academic_Program__c> programs;

        // Gets the number of students in a cohort (Enrolled or Graduated).
        public integer NumberOfCohortStudents {get; private set;}

        public TestRecordSet(){  
            // Add an Academic Program for the test Cohort
            this.programs = new List<Academic_Program__c>{
                new Academic_Program__c(
                    name = 'Internet MBA: One-Year',
                    Program_name_long__c = 'Internet MBA: One-Year')                       
            };

            insert this.programs;

            // Setup the test cohort      
            this.cohorts = new List<Cohort__c>{
                new Cohort__c(
                    name = 'I1MBAS13',
                    Start_Date__c = System.Today(),
                    Academic_Program__c = this.programs[0].Id)
            };

            insert this.cohorts;

            // Setup the test accounts and assign them to our cohort
            RecordType studentRecordType = [SELECT Id FROM RecordType WHERE Name = 'Graduate Student'];
            this.accounts = new List<Account>{
                new Account(
                    FirstName = 'Enrolled',
                    LastName = 'Test',
                    Cohort__c = this.cohorts[0].Id,
                    Enrollment_Status__c = 'Enrolled',
                    RecordTypeId = studentRecordType.Id),
                new Account(
                    FirstName = 'Graduated',
                    LastName = 'Test',
                    Cohort__c = this.cohorts[0].Id,
                    Enrollment_Status__c = 'Graduated'),
                new Account(
                    FirstName = 'Withdrew',
                    LastName = 'Test',
                    Cohort__c = this.cohorts[0].Id,
                    Enrollment_Status__c = 'Withdrew'),
                new Account(
                    FirstName = 'Denied',
                    LastName = 'Test',
                    Cohort__c = this.cohorts[0].Id,
                    Enrollment_Status__c = '')
            };

            this.NumberOfCohortStudents = 2;
            insert this.accounts;

            // Configure our test applications
            this.applications = new List<Opportunity>{
                new Opportunity(
                    name='Enrolled Application',
                    Account = this.accounts[0],       
                    CloseDate = System.Today(),
                    StageName = 'Enrolled',       
                    Test_Score_Type_for_Admission__c = 'Exempt', 
                    Academic_Program__c = this.programs[0].Id),
                new Opportunity(
                    name='Graduated Application',
                    Account = this.accounts[1],        
                    CloseDate = System.Today(),
                    StageName = 'Enrolled',       
                    Test_Score_Type_for_Admission__c = 'Exempt', 
                    Academic_Program__c = this.programs[0].Id),
                new Opportunity(
                    name='Withdrawn Application',
                    Account = this.accounts[2],       
                    CloseDate = System.Today(),
                    StageName = 'Enrolled',       
                    Test_Score_Type_for_Admission__c = 'Exempt', 
                    Academic_Program__c = this.programs[0].Id),
                new Opportunity(
                    name='Denied Application',
                    Account = this.accounts[3],       
                    CloseDate = System.Today(),
                    StageName = 'Denied',       
                    Academic_Program__c = this.programs[0].Id)
            };

            insert this.applications; 
        }
    }
}

How is it that in my unit test these are populated but null for my extension?

** Update ** Provided the full testing class

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

The issue is with your Opportunity test data set up.

For example, you have Account = this.accounts[1] which sets the relationship field. You never actually set the Id field, though.

Try setting the AccountId field on the Opportunities instead. That should work.

AccountId = this.accounts[1].Id.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x