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
}
}