Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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

share|improve this question
1  
please put your full test code for extension –  Mohith Shrivastava Dec 20 '12 at 18:53
    
You've added all your test data setup in the constructor of your utility class. I'm not sure if its failing because of that. Wonder if it changes when you move it to an instance method which is explicitly invoked. –  techtrekker Dec 20 '12 at 19:35

1 Answer 1

up vote 2 down vote accepted

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.

share|improve this answer
    
Rookie mistake on my part. Thank you! –  Mike Chale Dec 21 '12 at 12:34

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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