I have a visualforce page which is displaying a list of opportunities.

The page is working as expected - no problems.

The problem is that when testing I can't access the account which is associated with the opportunity. It displays on the visualforce page, but seemingly isn't set when running the test.

Controller:

public class showGuideExcel {

    public List <opportunity> oppList {get; set;}        


    public showGuideExcel () {
        string groupId = ApexPages.currentPage().getParameters().get('id');
        oppList = [SELECT account.name, account.website, account.billingCountry
               FROM opportunity WHERE group__c = :groupId and stageName = 'Contract Returned'];         
    }
}

Test:

@isTest
public class showGuideExcelTest {
    @isTest
    public static void retrieveCorrectInfo(){

        group__c testGroup = new group__c();
        insert testGroup;
        account testAccount = new Account(billingStreet ='testStreet',
                                          billingCity = 'Test City',
                                          billingState = 'Test State',
                                          billingCountry = 'Test Country',
                                          name = 'Test Account'
                                         );
        insert testAccount;
        opportunity testOpportunity = new Opportunity(stageName = 'Contract Returned',
                                                      name = 'Test Opportunity',
                                                      closeDate = System.today(),
                                                      group__c = testGroup.id,
                                                      account = testAccount
                                                     );
        insert testOpportunity;

        ApexPages.currentPage().getParameters().put('id', testGroup.id);

        System.debug(testAccount);
        System.debug(testOpportunity);
        System.debug(testOpportunity.account);
        // THE ABOVE ALL RETURN CORRECTLY
        showGuideExcel controller = new showGuideExcel();
        System.debug(controller.oppList);
        System.debug(controller.oppList[0]);
        System.debug(controller.oppList[0].account.name);
        System.assertEquals('Test Account', controller.oppList[0].account.name);
        // TEST FAILS - System.AssertException: Assertion Failed: Expected: Test Account, Actual: null
    }
}
up vote 3 down vote accepted

The form of DML you're using, where you assign an sObject to the relationship property, is designed for adding related records using an external Id.

opportunity testOpportunity = new Opportunity(stageName = 'Contract Returned',
                                              name = 'Test Opportunity',
                                              closeDate = System.today(),
                                              group__c = testGroup.id,
                                              account = testAccount
                                             );

To relate the Opportunity to the Account using the regular Id field, set

AccountId = testAccount.Id

that is, directly assign the Id of the related record to the lookup field.

See Inserting Related Records for more.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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