You need to sign in to do that
Don't have an account?
Shawn Reichner 29
Test class for VF Page Controller - HELP
Hello awesome devs...I have a good one here today!
The company has had a Consulting group create a VF page and controller to do the following function. It seems that thier Test class was removed from our environment somehow as this is in our production org, however no test class provides any code coverage any longer. I need to make a small edit to the VF page, and want to ensure that the controller has proper coverage after I make the small tweak, but again I can not find any test class that provides coverage. Thsi was done a few years back so the change sets that included this info has expired and we no longer have visibility into it. So I need to create a new test class, but am unsure how to for this piece of particular code.
Can anyone help to help me create a test class for the folllowing code?
On Object A - the new button is pressed, which navigates you to the VF page which allows you to either
A: select an existing account with a specific record type or:
B: if not found the next section of the VF page will allow you to create a new Account record to use for the custom object upon saving the VF page.
Here is the controller code for this VF page
The company has had a Consulting group create a VF page and controller to do the following function. It seems that thier Test class was removed from our environment somehow as this is in our production org, however no test class provides any code coverage any longer. I need to make a small edit to the VF page, and want to ensure that the controller has proper coverage after I make the small tweak, but again I can not find any test class that provides coverage. Thsi was done a few years back so the change sets that included this info has expired and we no longer have visibility into it. So I need to create a new test class, but am unsure how to for this piece of particular code.
Can anyone help to help me create a test class for the folllowing code?
On Object A - the new button is pressed, which navigates you to the VF page which allows you to either
A: select an existing account with a specific record type or:
B: if not found the next section of the VF page will allow you to create a new Account record to use for the custom object upon saving the VF page.
Here is the controller code for this VF page
public class new_testVfpage { Public Purchase_request__c pr{get;set;} Public string accountname{get;set;} Public string phonenumber{get;set;} Public string Billingstreet{get;set;} Public string Billingcity{get;set;} public string contactname{get;set;} public string contactemail{get;set;} Public String billingzipcode{get;set;} public Account account {get;set;} Public string testfield{get;set;} public boolean isExistError{get;set;} public boolean isNullError{get;set;} public new_testVfpage(ApexPages.StandardController sc) { pr= (Purchase_request__c )sc.getRecord(); account=new account(); pr.Renewal__c='Existing Vendor'; Id rtid= Schema.SObjectType.Purchase_request__c.getRecordTypeInfosByName().get('PR Existing Vendor, No V-Qs /w Submit').getRecordTypeId(); pr.recordtypeid =rtid; isExistError=false; } Public pagereference newsave(){ integer needDesc = 0; integer descMax = 0; testfield=pr.Vendor_Name__c; //Identifiy if Insert or Update Vendor Type of Record Type Id vendorId = Schema.SobjectType.Account.getRecordTypeInfosByName().get('Vendor').getRecordTypeId(); List<Account> accList = [SELECT Id FROM Account where Id = :pr.Vendor_Name__c AND RecordType.Id =: vendorId]; if(accList.IsEmpty()){//insert Id prrtid= Schema.SObjectType.Purchase_request__c.getRecordTypeInfosByName().get('PR All Questions /w Submit Button').getRecordTypeId(); pr.RecordTypeId = prrtid; //'01270000000UQfyAAG'; //PR All Questions /w Submit Button }else{//Update Id prrtid = Schema.SObjectType.Purchase_request__c.getRecordTypeInfosByName().get('PR Existing Vendor, No V-Qs /w Submit').getRecordTypeId(); pr.RecordTypeId = prrtid; //'01270000000UQg1AAG'; //PR Existing Vendor, No V-Qs /w Submit } //CheckVendor Name is Empty if(testfield == null || testfield ==''){ //When Account Name has value if(accountname!= '' && accountname!= null){ account.name=accountname; isExistError=false; //New Vendor will check the following fields if((account.Will_vendor_store_NPPI_or_PCI_data__c != 'Yes' && account.Will_vendor_store_NPPI_or_PCI_data__c != 'No')){ ApexPages.addMessage(new apexpages.message(ApexPages.Severity.ERROR, 'Will vendor store NPPI or PCI data? is required')); isExistError=true; } if((account.Will_vendor_store_PHI_data__c != 'Yes' && account.Will_vendor_store_PHI_data__c != 'No')){ ApexPages.addMessage(new apexpages.message(ApexPages.Severity.ERROR, 'Will vendor store PHI data?')); isExistError=true; } if((account.Will_vendor_store_Armor_corporate_data__c != 'Yes' && account.Will_vendor_store_Armor_corporate_data__c != 'No')){ ApexPages.addMessage(new apexpages.message(ApexPages.Severity.ERROR, 'Will vendor store Armor corporate data? is required')); isExistError=true; } if((account.Will_vendor_access_Armor_or_Cust_data__c != 'Yes' && account.Will_vendor_access_Armor_or_Cust_data__c != 'No')){ ApexPages.addMessage(new apexpages.message(ApexPages.Severity.ERROR, 'Will vendor access Armor or Customer Data? is required')); isExistError=true; } if((account.Are_vendor_services_Critical_to_Armor__c!= 'Yes' && account.Are_vendor_services_Critical_to_Armor__c!= 'No')){ ApexPages.addMessage(new apexpages.message(ApexPages.Severity.ERROR, 'Are vendor services critical to Armor? is required')); isExistError=true; } //removes error if all the condition of required fields for new vendor is Yes if((account.Will_vendor_store_NPPI_or_PCI_data__c != '' && account.Will_vendor_store_NPPI_or_PCI_data__c != null && account.Will_vendor_store_NPPI_or_PCI_data__c != '--None--') && (account.Will_vendor_store_PHI_data__c != '' && account.Will_vendor_store_PHI_data__c != null && account.Will_vendor_store_PHI_data__c != '--None--') && (account.Will_vendor_store_Armor_corporate_data__c != '' && account.Will_vendor_store_Armor_corporate_data__c != null && account.Will_vendor_store_Armor_corporate_data__c != '--None--') && (account.Will_vendor_access_Armor_or_Cust_data__c != '' && account.Will_vendor_access_Armor_or_Cust_data__c != null && account.Will_vendor_access_Armor_or_Cust_data__c != '--None--') && (account.Are_vendor_services_Critical_to_Armor__c != '' && account.Are_vendor_services_Critical_to_Armor__c != null && account.Are_vendor_services_Critical_to_Armor__c != '--None--')){ isExistError=false; } //Error if NPPI/PCI desc is null when account.Will_vendor_store_PHI_data__c value is yes if(account.Will_vendor_store_NPPI_or_PCI_data__c == 'Yes' ){ if(account.NPPI_PCI_If_Yes_enter_description__c != null){ isExistError=false; isNullError=false; }else{ ApexPages.addMessage(new apexpages.message(ApexPages.Severity.ERROR, 'NPPI/PCI is set to Yes please enter description')); isExistError=true; isNullError=true; } }else{ if(account.Will_vendor_store_NPPI_or_PCI_data__c == 'No'){ isExistError=false; isNullError=false; }else{ isExistError=true; } } //Error if PHI desc is null when account.Will_vendor_store_PHI_data__c value is yes //if(account.Will_vendor_store_PHI_data__c == 'Yes' ){ if(account.Will_vendor_store_PHI_data__c == 'Yes' ){ if(account.PHI_If_Yes_enter_description__c != null){ if(isNullError==false){ isExistError=false; isNullError=false; } }else{ ApexPages.addMessage(new apexpages.message(ApexPages.Severity.ERROR, 'PHI is set to Yes please enter description')); isExistError=true; isNullError=true; } }else{ if(account.Will_vendor_store_PHI_data__c == 'No'){ if(isNullError==false){ isExistError=false; isNullError=false; } }else{ isExistError=true; } } //Error if Corp Data desc is null when Corp Data value is yes if(account.Will_vendor_store_Armor_corporate_data__c == 'Yes' ){ if(account.Corp_Data_If_Yes_enter_description__c != null){ if(isNullError==false){ isExistError=false; isNullError=false; } }else{ ApexPages.addMessage(new apexpages.message(ApexPages.Severity.ERROR, 'Corp Data is set to Yes please enter description')); isExistError=true; isNullError=true; } }else{ if(account.Will_vendor_store_Armor_corporate_data__c == 'No'){ if(isNullError==false){ isExistError=false; isNullError=false; } }else{ isExistError=true; } } //Error if Access Data desc is null when Access Data value is yes if(account.Will_vendor_access_Armor_or_Cust_data__c == 'Yes' ){ if(account.Cust_Data_If_Yes_enter_description__c != null){ if(isNullError==false){ isExistError=false; isNullError=false; } }else{ ApexPages.addMessage(new apexpages.message(ApexPages.Severity.ERROR, 'Access Data is set to Yes please enter description')); isExistError=true; isNullError=true; } }else{ if(account.Will_vendor_access_Armor_or_Cust_data__c == 'No'){ if(isNullError==false){ isExistError=false; isNullError=false; } }else{ isExistError=true; } } //Error if Critical desc is null when account.Are_vendor_services_Critical_to_Armor__c value is yes if(account.Are_vendor_services_Critical_to_Armor__c == 'Yes' ){ if(account.Critical_If_Yes_enter_description__c != null){ if(isNullError==false){ isExistError=false; isNullError=false; } }else{ ApexPages.addMessage(new apexpages.message(ApexPages.Severity.ERROR, 'Critical is set to Yes please enter description')); isExistError=true; isNullError=true; } }else{ if(account.Are_vendor_services_Critical_to_Armor__c == 'No'){ if(isNullError==false){ isExistError=false; isNullError=false; } }else{ isExistError=true; } } //removes error if all required fields are not null if((account.Will_vendor_store_NPPI_or_PCI_data__c == 'Yes') && (account.Will_vendor_store_PHI_data__c == 'Yes' ) && (account.Will_vendor_store_Armor_corporate_data__c == 'Yes') && (account.Will_vendor_access_Armor_or_Cust_data__c == 'Yes') && (account.Are_vendor_services_Critical_to_Armor__c == 'Yes')){ //Description null check if((account.NPPI_PCI_If_Yes_enter_description__c != null && account.NPPI_PCI_If_Yes_enter_description__c != '') &&(account.PHI_If_Yes_enter_description__c != null && account.PHI_If_Yes_enter_description__c != '') &&(account.Corp_Data_If_Yes_enter_description__c != null && account.Corp_Data_If_Yes_enter_description__c != '') &&(account.Cust_Data_If_Yes_enter_description__c != null && account.Cust_Data_If_Yes_enter_description__c != '') &&(account.Critical_If_Yes_enter_description__c != null && account.Critical_If_Yes_enter_description__c != '') ){ isExistError=false; }else{ isExistError=true; } } }else{ ApexPages.addMessage(new apexpages.message(ApexPages.Severity.ERROR, 'Account Name is required')); isExistError=true; } Id accrtid= Schema.SObjectType.Account.getRecordTypeInfosByName().get('Vendor').getRecordTypeId(); account.recordtypeid=accrtid; contact c=new contact(); if(contactname!= '' && contactname !=null){ c.lastName=contactname; } if(isExistError==true){ return null; }else{ upsert account; Upsert Pr; } if(contactname != '' && contactname !=null){ c.accountid=account.id; c.LeadSource='PR VF Page - Manually'; insert c; } pr.Vendor_Name__c=account.id; pr.Renewal__c='New Vendor'; upsert Pr; } if(accountname!= '' && accountname!= null){ upsert Pr; return new PageReference('/'+pr.Id+'/e?retURL=%2F'+pr.Id); }else{ if(testfield == null || testfield ==''){ return null; }else{ upsert Pr; return new PageReference('/'+pr.Id+'/e?retURL=%2F'+pr.Id); } } } }
The class seems impressive (big) but it is quite simple. It is an extension of a standard controller.
The class rules just three objects: a Purchase_request__c (master object for the standard controller) a related account (List<Account> accList = [SELECT Id FROM Account where Id = :pr.Vendor_Name__c AND RecordType.Id =: vendorId];) and a contact for this account.
The main problem is the combinations of variables for the errors according the values of fields of account. The test class will contains several creations of accounts for having all these combinations (but by copy/paste, it is not so difficult).
This blog is a reference: Jeff Douglas
http://blog.jeffdouglas.com/2010/06/02/testing-salesforce-com-controller-extensions/
MyController ext; => new_testVfpage ext;
MasterObject__c masterObject => Purchase_request__c pr
ApexPages.StandardController con = new ApexPages.StandardController(masterObject);
ext = new MyController(con);
will become:
// creations of objects Purchase_request__c pr and the Account related to pr
Account acc = new Account( ... );
insert acc;
Purchase_request__c pr = new Purchase_request__c( ...Vendor_Name__c = acc.id ... );
insert pr;
ApexPages.StandardController con = new ApexPages.StandardController(pr);
new_testVfpage ext = new new_testVfpage(con);
ext.accountname = 'my account name';
ext. newsave();
Other developers will perhaps post a more detailed solution but the principle is always the same when you have a template (here Jeff Douglas' example)
All Answers
The class seems impressive (big) but it is quite simple. It is an extension of a standard controller.
The class rules just three objects: a Purchase_request__c (master object for the standard controller) a related account (List<Account> accList = [SELECT Id FROM Account where Id = :pr.Vendor_Name__c AND RecordType.Id =: vendorId];) and a contact for this account.
The main problem is the combinations of variables for the errors according the values of fields of account. The test class will contains several creations of accounts for having all these combinations (but by copy/paste, it is not so difficult).
This blog is a reference: Jeff Douglas
http://blog.jeffdouglas.com/2010/06/02/testing-salesforce-com-controller-extensions/
MyController ext; => new_testVfpage ext;
MasterObject__c masterObject => Purchase_request__c pr
ApexPages.StandardController con = new ApexPages.StandardController(masterObject);
ext = new MyController(con);
will become:
// creations of objects Purchase_request__c pr and the Account related to pr
Account acc = new Account( ... );
insert acc;
Purchase_request__c pr = new Purchase_request__c( ...Vendor_Name__c = acc.id ... );
insert pr;
ApexPages.StandardController con = new ApexPages.StandardController(pr);
new_testVfpage ext = new new_testVfpage(con);
ext.accountname = 'my account name';
ext. newsave();
Other developers will perhaps post a more detailed solution but the principle is always the same when you have a template (here Jeff Douglas' example)
Thank you again for your help. I am still confused however, as I am attempting to go your toute, but I can only get 20 percent code coverage. What am i doing incorrectly?
Here is the class again and the following lines do not have code coverage...also following is the test class as I currently have it.
Lines 5,6,7,9,10,14,47 through 215, 230,235,236,239 through 247 and lastly 254.
Please help....
Test class so far....
Hi Shawn
As you have posted a new question with your code, you will get many answers.
public Account account {get;set;} // fields on screen not already saved
Account acc2 = new Account( . myfields initialisation ie: Will_vendor_store_Armor_corporate_data__c == 'Yes'....);
// insert not needed for acc2 but ext4.account = acc2; is mandatory before the call ext4.newsave();
ApexPages.StandardController con4 = new ApexPages.StandardController(pr4);
new_testVFpage ext4 = new new_testVFpage(con4);
ext4.account = acc2; // calls the setter of account inside the controller. so all the fields account.myfields will be filed
ext4.newsave();
Thank you so much for your help, I really appreciate it, I just need a little more info...
Shawn
Shawn