You need to sign in to do that
Don't have an account?
Shekhar Dautpure
testclass for visualforce page
Dear All
I have vf page and custom extension controller. All is fine , however I need to migrate it to production, and i am having problem with code coverage. I have code coverage of 18%, and somehow its not incresing.
can you please advise.
My custom extension is :
--
--
My testclass is :
thanks
Shekhar
I have vf page and custom extension controller. All is fine , however I need to migrate it to production, and i am having problem with code coverage. I have code coverage of 18%, and somehow its not incresing.
can you please advise.
My custom extension is :
--
global without sharing class ContactSlim_Edelivery_Extension { //private final Contact_Slim__c cont; --1st change public Contact_Slim__c cont; private ApexPages.StandardController sc; public ID CP_Pri_Inv_Signer = null; public ID CP_Sec_Inv_Signer = null; public ID CP_Pri_OC_Signer = null; public ID CP_Sec_OC_Signer = null; public ID CP_General_Signer = null; public boolean hasBeenSubmited {get; set;} public final ID AccntSlimID {get; set;} public final String UserLocale {get; set;} public List<multiRowContact> RelatedContactPersons {get; set;} public boolean displayPopup {get; set;} public boolean displayPopup_OC {get; set;} public String AccountName {get; set;} public String AccountSAPID {get; set;} public String AccountSFDCID {get; set;} public String AccountCountryCode {get; set;} private String AttachmentURL ='https://crmprod.force.com/'; public ContactSlim_Edelivery_Extension(ApexPages.StandardController stdController) { sc = stdController; //Orginal Line of code //stdController.addFields(new List<String>{'Name','AccountId__c','Contact__c','Email__c','Primary_Invoice_Recipient__c','Primary_Order_Confirmation_Recipient__c','Secondary_Invoice_Recipient__c','Secondary_Order_Confirmation_Recipient__c'}); //Updated line of code if(!test.isRunningTest()) { stdController.addFields(new List<String>{'Name','AccountId__c','Contact__c','Email__c','Primary_Invoice_Recipient__c','Primary_Order_Confirmation_Recipient__c','Secondary_Invoice_Recipient__c','Secondary_Order_Confirmation_Recipient__c'}); } this.cont = (Contact_Slim__c)stdController.getRecord(); AccntSlimID = cont.AccountId__c; //SFDC ID of Account_Slim Account_Slim__c a = [Select Id,Name,Account__c,Account_Number__c,Country_Code__c from Account_Slim__c where Id =: AccntSlimID limit 1]; AccountName = a.Name; AccountSAPID = a.Account_Number__c; AccountSFDCID = a.Account__c; //Actual SFDC of Account UserLocale = ApexPages.currentPage().getParameters().get('Locale'); AccountCountryCode = UserLocale; LoadData(); hasBeenSubmited = false; } private void LoadData() { RelatedContactPersons = new List<multiRowContact>(); for (List<Contact_Slim__c> cs :[Select Id , Name , Email__c,Primary_Invoice_Recipient__c,Primary_Order_Confirmation_Recipient__c,Secondary_Invoice_Recipient__c, Secondary_Order_Confirmation_Recipient__c, FirstName__c, LastName__c from Contact_Slim__c where AccountId__c =: cont.AccountId__c and Email__c != '' and EDeliveryNotEnabled__c=false limit 900]) { for (Contact_Slim__c c : cs) { multiRowContact MRC = new multiRowContact(); MRC.ID = c.ID; MRC.Name = c.Name; MRC.Email = c.Email__c; MRC.Pri_Invoice_Flag = c.Primary_Invoice_Recipient__c; MRC.Pri_OC_Flag = c.Primary_Order_Confirmation_Recipient__c; MRC.Sec_Invoice_Flag = c.Secondary_Invoice_Recipient__c; MRC.Sec_OC_Flag = c.Secondary_Order_Confirmation_Recipient__c; MRC.FirstName = c.FirstName__c; MRC.LastName = c.LastName__c; RelatedContactPersons.add(MRC); } } } public void showPopup() { displayPopup = true; } public void showPopup_OC() { displayPopup_OC = true; } public void closePopup() //Method executed on clicking OK on Invoice Popup applet { saveNewContacts(); for (multiRowContact MRC : RelatedContactPersons) { MRC.Save_Invoice(); } UpdateAccountEdeliveryOptions(); LoadData(); displayPopup = false; } public void closePopup_OC() //Method executed on clicking OK on OC Popup applet { saveNewContacts(); for (multiRowContact MRC : RelatedContactPersons) { MRC.Save_OC(); } UpdateAccountEdeliveryOptions(); LoadData(); displayPopup_OC = false; } public void closePopup_Cancel_OC() //Method executed on clicking Cancel on OC Popup applet { displayPopup_OC = false; cleanNewContacts(); } public void UpdateAccountEdeliveryOptions() { Account_Slim__c accountToUpdate; try{ for (List<Contact_Slim__c> cp :[Select Name,Id,EdeliverySigner__c,Primary_Invoice_Recipient__c,Primary_Order_Confirmation_Recipient__c,Secondary_Invoice_Recipient__c,Secondary_Order_Confirmation_Recipient__c from Contact_Slim__c where AccountId__c =: AccntSlimID and Email__c != '' and EDeliveryNotEnabled__c= false limit : (limits.getLimitQueryRows()-limits.getQueryRows())]) { for (Contact_Slim__c c : cp) { if(c.EdeliverySigner__c==true) { CP_General_Signer=c.Id; } if(c.Primary_Invoice_Recipient__c==true) { CP_Pri_Inv_Signer=c.Id; } if(c.Secondary_Invoice_Recipient__c==true) { CP_Sec_Inv_Signer=c.Id; } if(c.Primary_Order_Confirmation_Recipient__c==true) { CP_Pri_OC_Signer=c.Id; } if(c.Secondary_Order_Confirmation_Recipient__c==true) { CP_Sec_OC_Signer=c.Id; } } } accountToUpdate=[SELECT EdeliverySigner__c,Primary_Invoice_Recipient__c,Primary_Order_Confirmation_Recipient__c,Secondary_Invoice_Recipient__c,Secondary_Order_Confirmation_Recipient__c FROM Account_Slim__c WHERE id =: AccntSlimID LIMIT :(limits.getLimitQueryRows()-limits.getQueryRows())]; if(CP_General_Signer!=null) { accountToUpdate.EdeliverySigner__c=CP_General_Signer; } if(CP_Pri_Inv_Signer!=null) { accountToUpdate.Primary_Invoice_Recipient__c=CP_Pri_Inv_Signer; } if(CP_Sec_Inv_Signer!=null) { accountToUpdate.Secondary_Invoice_Recipient__c=CP_Sec_Inv_Signer; } if(CP_Pri_OC_Signer!=null) { accountToUpdate.Primary_Order_Confirmation_Recipient__c=CP_Pri_OC_Signer; } if(CP_Sec_OC_Signer!=null) { accountToUpdate.Secondary_Order_Confirmation_Recipient__c=CP_Sec_OC_Signer; } }catch(DmlException e){ System.debug('An unexpected error has occurred while updating account with CP info : ' + e.getMessage()); }finally{ update accountToUpdate; } } public void closePopup_Cancel() //Method executed on clicking Cancel on Invoice Popup applet { cleanNewContacts(); displayPopup = false; } public void closePopup_New_CP() //Method executed on clicking New Contact any Popup applet { addNewContact(); } private void addNewContact() { multiRowContact MRC = new multiRowContact(); MRC.InsertMode = true; RelatedContactPersons.add(MRC); } private void cleanNewContacts() { Integer j = 0; while (j < RelatedContactPersons.size()) { if(RelatedContactPersons.get(j).ID == null) { RelatedContactPersons.remove(j); }else { j++; } } } public void saveNewContacts() { List<Contact_Slim__c> newContacts = new List<Contact_Slim__c>(); for (multiRowContact MRC: RelatedContactPersons) { if (MRC.InsertMode) { Contact_Slim__c nCon = new Contact_Slim__c(); setContact(nCon, MRC); if (isValidContact(nCon)) newContacts.add(nCon); } } if (newContacts.size()> 0 ) insert newContacts; } private void setContact(Contact_Slim__c con, multiRowContact MRC) { con.FirstName__c = MRC.FirstName; con.LastName__c = MRC.LastName; con.Email__c = MRC.Email; con.Primary_Invoice_Recipient__c = MRC.Pri_Invoice_Flag == null? false: MRC.Pri_Invoice_Flag; con.Primary_Order_Confirmation_Recipient__c = MRC.Pri_OC_Flag == null? false: MRC.Pri_OC_Flag; con.Secondary_Invoice_Recipient__c = MRC.Sec_Invoice_Flag== null? false: MRC.Sec_Invoice_Flag; con.Secondary_Order_Confirmation_Recipient__c = MRC.Sec_OC_Flag == null? false: MRC.Sec_OC_Flag; con.AccountId__c = AccntSlimID; } private Boolean isValidContact(Contact_Slim__c con) { if (con.Email__c == '' || con.FirstName__c == '' || con.LastName__c == '') return false; return true; } public void AttachAgreement() { String URL1=AttachmentURL+UserLocale+'/apex/Edelivery_Agreement?Locale='+UserLocale+'&id='+AccntSlimID; System.debug('URL -'+URL1); PageReference pdf = new PageReference(URL1); Attachment attach = new Attachment(); Blob body; try { body = pdf.getContentAsPDF(); } catch(VisualforceException e) { body = Blob.valueOf('Error encountered while generating agreement. Contact Ecco customer care or your contact person for resolution.'); } attach.Body = body; // add the user entered name attach.Name = 'Agreement for Edelivery of Invoices and Order Confirmation.pdf'; attach.IsPrivate = false; // attach the pdf to the account attach.ParentId = AccountSFDCID; insert attach; } public PageReference save() { //sc.save(); AttachAgreement(); hasBeenSubmited = true; return null; } //Inner class // private class multiRowContact -- 2nd change public class multiRowContact { public String ID {get; set;} public String Name { get; set; } public String Email { get; set; } public Boolean Pri_Invoice_Flag { get; set; } public Boolean Sec_Invoice_Flag { get; set; } public Boolean Pri_OC_Flag { get; set; } public Boolean Sec_OC_Flag { get; set; } public Boolean InsertMode { get; set; } public String FirstName {get; set;} public String LastName {get; set;} public multiRowContact() { InsertMode = false; } public void Save_Invoice() { if (InsertMode == false) { Contact_Slim__c c = [SELECT c.Primary_Invoice_Recipient__c,c.Secondary_Invoice_Recipient__c FROM Contact_Slim__c c WHERE c.ID = :ID LIMIT 1]; c.Primary_Invoice_Recipient__c = Pri_Invoice_Flag; c.Secondary_Invoice_Recipient__c = Sec_Invoice_Flag; update c; } } public void Save_OC() { if (InsertMode == false) { Contact_Slim__c c = [SELECT c.Primary_Order_Confirmation_Recipient__c,c.Secondary_Order_Confirmation_Recipient__c FROM Contact_Slim__c c WHERE c.ID = :ID LIMIT 1]; c.Primary_Order_Confirmation_Recipient__c = Pri_OC_Flag; c.Secondary_Order_Confirmation_Recipient__c = Sec_OC_Flag; update c; } } } }
--
My testclass is :
@isTest(seeAllData=false) private class Testcase_ContSlim_Edelivery_Exten { static testMethod void myUnitTest() { Test.startTest(); //************************Instialisation************************************ Account A1 = New Account(); A1.Name = 'TestConSEdeliveryExten'; A1.Email__c='a@TestConSEdeliveryExten.com'; insert A1; Account A2 = [Select Id from Account where Name =: 'TestConSEdeliveryExten' limit 1]; String AccId=A2.Id; Contact C1 = New Contact(); C1.AccountId = AccId; C1.FirstName = 'TestConSEdeliveryExten'; C1.LastName = 'TestConSEdeliveryExten'; C1.Email='contact@TestConSEdeliveryExten.com'; insert c1; Contact c2 = [Select Id from Contact where FirstName ='TestConSEdeliveryExten' limit 1]; String ConId=c2.Id; Account_Slim__c ac1 = New Account_Slim__c(); ac1.Account__c=AccId; ac1.Account_Number__c='101010'; ac1.Country_Code__c='NL'; ac1.Name='TestConSEdeliveryExten'; insert ac1; //Account_Slim__c ac2 = [Select Id from Account_Slim__c where Name = 'TestConSEdeliveryExten' limit 1]; String AccSlimId = ac1.id; Contact_Slim__c conSlim1 = new Contact_Slim__c(); conSlim1.AccountId__c=AccSlimId; conSlim1.AccountSFDCID__c=AccId; conSlim1.Contact__c=ConId; conSlim1.Name = 'TestConSEdeliveryExten'; insert conSlim1; Contact_Slim__c conSlim2 = [Select Id from Contact_Slim__c where Name = 'TestConSEdeliveryExten' limit 1]; String conSlimId= conSlim2.Id; //************************Instialisation************************************ PageReference pageRef = new PageReference('/apex/Edelivery_Survey_UK'); Test.setCurrentPageReference(pageRef); ApexPages.CurrentPage().getparameters().put('Locale', 'UK'); ApexPages.CurrentPage().getparameters().put('Id',conSlim1.Id); ApexPages.StandardController sc = new ApexPages.standardController(conSlim1); ContactSlim_Edelivery_Extension ec = new ContactSlim_Edelivery_Extension(sc); ec.cont = conSlim1; ContactSlim_Edelivery_Extension.multiRowContact mrc = new ContactSlim_Edelivery_Extension.multiRowContact(); //ContactSlim_Edelivery_Extension ec = new ContactSlim_Edelivery_Extension(stdController); //ec.cont=conSlim1; //ContactSlim_Edelivery_Extension.multiRowContact mrc = new ContactSlim_Edelivery_Extension.multiRowContact(); Test.stopTest(); } }
thanks
Shekhar
I would suggest that you read over the following article [1] to help you plan your tests.
[1] http://blog.deadlypenguin.com/blog/testing/strategies/