You need to sign in to do that
Don't have an account?

Apex Test Class Page Parameters
Hello all -
I created a test opportunity:
Opportunity Opp = new Opportunity();
Opp.Name = 'Test Opportiunity ';
Opp.AccountId = acct.id;
Opp.Business_Line__c = 'Capital Markets';
Opp.Result_Reason__c = 'Amenities';
Opp.CloseDate = System.today();
Opp.StageName = 'Qualified';
Opp.LeadSource = 'Web';
Opp.Type = 'New Customer - New Business';
Opp.Product_Type_Interests__c = 'ActiveDisclosure';
insert Opp;
After this I'm doing a System.assert('true,Opp.Id==null). The test class passes with this assert? I'm not sure why? The opportunity should have a ID I believe even within the test class.
Following to this code, I'm setting the id as a page parameter as below:
PageReference myVfPage = Page.AssociateECP;
Test.setCurrentPage(myVfPage);
ApexPages.StandardController sc = new ApexPages.StandardController(Opp);
AssociateECPController ac = new AssociateECPController(sc);
// Put Id into the current page Parameters
ApexPages.currentPage().getParameters().put('id',Opp.Id);
String id = ApexPages.currentPage().getParameters().get('id');
system.assert(true,id==null); --> Again I want to set the id as the Opp.Id so that I can cover the code in my original class
Please let me know what steps I'm missing. Thank you
I created a test opportunity:
Opportunity Opp = new Opportunity();
Opp.Name = 'Test Opportiunity ';
Opp.AccountId = acct.id;
Opp.Business_Line__c = 'Capital Markets';
Opp.Result_Reason__c = 'Amenities';
Opp.CloseDate = System.today();
Opp.StageName = 'Qualified';
Opp.LeadSource = 'Web';
Opp.Type = 'New Customer - New Business';
Opp.Product_Type_Interests__c = 'ActiveDisclosure';
insert Opp;
After this I'm doing a System.assert('true,Opp.Id==null). The test class passes with this assert? I'm not sure why? The opportunity should have a ID I believe even within the test class.
Following to this code, I'm setting the id as a page parameter as below:
PageReference myVfPage = Page.AssociateECP;
Test.setCurrentPage(myVfPage);
ApexPages.StandardController sc = new ApexPages.StandardController(Opp);
AssociateECPController ac = new AssociateECPController(sc);
// Put Id into the current page Parameters
ApexPages.currentPage().getParameters().put('id',Opp.Id);
String id = ApexPages.currentPage().getParameters().get('id');
system.assert(true,id==null); --> Again I want to set the id as the Opp.Id so that I can cover the code in my original class
Please let me know what steps I'm missing. Thank you
All Answers
Use below code for test class
Test.setCurrentPageReference(new PageReference('Page.myPage'));
System.currentPageReference().getParameters().put('id', idVariable);
http://salesforce.stackexchange.com/questions/23670/how-to-set-the-controller-id-for-the-test-code
// Create test account
Account a = new Account(Name='Test Account',BillingCity='Warrenville',BillingCountry='USA',Business_Line__c='Capital Markets');
insert a;
a = [SELECT Id,Name,GUP_Number__c,Business_Line__c FROM Account a WHERE Id =: a.Id];
system.assert(true,a.Id);
// Create GUP Parent Account
Account a1 = new Account(Name='Test Account1',BillingCity='Warrenville',BillingCountry='USA',Business_Line__c='Capital Markets');
a1 = [select id from Account where id = :a1.Id];
system.assert(true,a1.Id);
//Create test Opportunity
Opportunity Opp = new Opportunity();
Opp.Name = 'Test Opportiunity ';
Opp.AccountId = a.id;
Opp.Business_Line__c = 'Capital Markets';
Opp.Result_Reason__c = 'Amenities';
Opp.CloseDate = System.today();
Opp.StageName = 'Qualified';
Opp.LeadSource = 'Web';
Opp.Type = 'New Customer - New Business';
Opp.Product_Type_Interests__c = 'ActiveDisclosure';
insert Opp;
Opp = [select id from Opportunity where id = :Opp.Id];
system.assert(true,Opp.Id==null);
// update GUP Parent account as a1 on a
a.GUP_GUID__c = a1.Id;
update a;
PageReference myVfPage = Page.AssociateECP;
Test.setCurrentPage(myVfPage);
ApexPages.StandardController sc = new ApexPages.StandardController(Opp);
AssociateECPController ac = new AssociateECPController(sc);
// Put Id into the current page Parameters
ApexPages.currentPage().getParameters().put('id',Opp.Id);
String id = ApexPages.currentPage().getParameters().get('id');
system.assert(true,id==null);
system.assert(Opp.Id != null,Opp.Id); // when using assert, first parameter is your conditon and 2nd is any message.
or use system.assertEquals(true,opp.Id != null)
If I used system.assertEquals(true,opp.Id != null).. the test class doesnt fail.. that means opportunity is inserted..
but why I am not unable to set the ID right to the apex page parameter?
PageReference myVfPage = Page.AssociateECP;
Test.setCurrentPage(myVfPage);
ApexPages.currentPage().getParameters().put('id',Opp.Id);
String id = ApexPages.currentPage().getParameters().get('id');
system.assertEquals(true,id!=null);
ApexPages.StandardController sc = new ApexPages.StandardController(Opp);
AssociateECPController ac = new AssociateECPController(sc);
Its now not even hitting AssociateECPController... showing 0% code coverage...
// Create test account
Account a = new Account(Name='Test Account',BillingCity='Warrenville',BillingCountry='USA',Business_Line__c='Capital Markets');
insert a;
a = [SELECT Id,Name,GUP_Number__c,Business_Line__c FROM Account a WHERE Id =: a.Id];
system.assert(true,a.Id);
// Create GUP Parent Account
Account a1 = new Account(Name='Test Account1',BillingCity='Warrenville',BillingCountry='USA',Business_Line__c='Capital Markets');
a1 = [select id from Account where id = :a1.Id];
system.assert(true,a1.Id);
//Create test Opportunity
Opportunity Opp = new Opportunity();
Opp.Name = 'Test Opportiunity ';
Opp.AccountId = a.id;
Opp.Business_Line__c = 'Capital Markets';
Opp.Result_Reason__c = 'Amenities';
Opp.CloseDate = System.today();
Opp.StageName = 'Qualified';
Opp.LeadSource = 'Web';
Opp.Type = 'New Customer - New Business';
Opp.Product_Type_Interests__c = 'ActiveDisclosure';
insert Opp;
Opp = [select id from Opportunity where id = :Opp.Id];
system.assertEquals(true,opp.Id != null);
PageReference myVfPage = Page.AssociateECP;
Test.setCurrentPageReference(myVfPage); // use setCurrentPageReference,
ApexPages.currentPage().getParameters().put('id',Opp.Id);
String id = ApexPages.currentPage().getParameters().get('id');
system.assertEquals(true,id!=null);
ApexPages.StandardController sc = new ApexPages.StandardController(Opp);
AssociateECPController ac = new AssociateECPController(sc);
But its not even hitting AssociateECPController.. When I test run from Developer Console.. it shows 0% code coverage.
String paramId = ApexPages.currentPage().getParameters().get('id');
system.assertEquals(true,paramId!=null);
private class AssociateECPControllerTest {
static void testAssociateECPController() {
// Create test account
Account a = new Account(Name='Test Account',BillingCity='Warrenville',BillingCountry='USA',Business_Line__c='Capital Markets');
insert a;
a = [SELECT Id FROM Account a WHERE Id =: a.Id];
system.assert(true,a.Id);
// Create GUP Parent Account
Account a1 = new Account(Name='Test Account1',BillingCity='Warrenville',BillingCountry='USA',Business_Line__c='Capital Markets');
a1 = [select id from Account where id = :a1.Id];
system.assert(true,a1.Id);
//Create test Opportunity
Opportunity Opp = new Opportunity();
Opp.Name = 'Test Opportiunity ';
Opp.AccountId = a.id;
Opp.Business_Line__c = 'Capital Markets';
Opp.Result_Reason__c = 'Amenities';
Opp.CloseDate = System.today();
Opp.StageName = 'Qualified';
Opp.LeadSource = 'Web';
Opp.Type = 'New Customer - New Business';
Opp.Product_Type_Interests__c = 'ActiveDisclosure';
insert Opp;
Opp = [select id from Opportunity where id = :Opp.Id];
system.assertEquals(true,Opp.Id != null);
// update GUP Parent account as a1 on a
a.GUP_GUID__c = a1.Id;
update a;
//Create test ECP
ECP__c ecp = new ECP__c();
ecp.GUP_GUID__c = a1.Id;
ecp.Status_Internal__c = 'Active';
insert ecp;
system.assert(true,ecp.OwnerId==Opp.OwnerId);
PageReference myVfPage = Page.AssociateECP;
Test.setCurrentPageReference(myVfPage); // use setCurrentPageReference,
ApexPages.currentPage().getParameters().put('id',Opp.Id);
String paramId = ApexPages.currentPage().getParameters().get('id');
system.assertEquals(true,paramId!=null);
ApexPages.StandardController sc = new ApexPages.StandardController(Opp);
AssociateECPController ac = new AssociateECPController(sc);
}
}
This is my controller:
public class AssociateECPController {
public list<ECPWrapper> associateECPWrapperList {get; set;}
Opportunity oppRecord;
ECP__c selectedECP;
list<ECP__c> ecpList = new list<ECP__c>();
list<Opportunity> oppList = new list<Opportunity>();
/********************************************************************************************************
Constructor
*********************************************************************************************************/
public AssociateECPController (ApexPages.StandardController controller) {
associateECPWrapperList = new list<ECPWrapper>();
initialise();
}
/********************************************************************************************************
Method to get ECP List from AccountMasterHelper
*********************************************************************************************************/
private void initialise() {
string currentOppty = ApexPages.currentPage().getParameters().get('id');
if(currentOppty != Null) {
oppList.addAll([SELECT OwnerId,GUP_GUID__c,WCSS_ECP__c,WCSS_Corporate__c,WCSS_Bill_To__c,GUID__c FROM Opportunity where Id = :currentOppty]);
if(oppList.size() > 0) {
oppRecord = oppList[0];
// get the ecp information
ecpList = ECPHelper.getECPList(oppList);
if(ecpList.size()== 0){
ApexPages.Message msg = new Apexpages.Message(ApexPages.Severity.Error,Label.No_Matching_ECPs);
ApexPages.addmessage(msg);
}
else {
for(ECP__c ecp : ecpList) {
associateECPWrapperList.add(new ECPWrapper(ecp));
}
}
}
}
}
/*****************************************************************************************************************
Wrapper class
******************************************************************************************************************/
public class ECPWrapper {
public ECP__c ecp {get; set;}
public Boolean selected {get; set;}
// constructor
private ECPWrapper(ECP__c ecp) {
this.ecp = ecp;
this.selected = false;
}
}
}
The test class doesnt fail... it passes with 0% coverage