You need to sign in to do that
Don't have an account?
syed akram
how to write test class for page Reference in salesforce?
Controller class Bold Text lines are not covering
public class AddPaymentController {
public Payment__C paymentObj {get;set;}
Public String invId {get;set;}
public AddPaymentController(ApexPages.StandardController controller) {
if(paymentObj == null) {
paymentObj = new Payment__C();
String invId = ApexPages.CurrentPage().getParameters().get('invid');
if(invId !=null) {
Invoice__c invObj = [Select Prospect_Client__r.ShippingPostalCode,Prospect_Client__r.ShippingState,Prospect_Client__r.Cell_Phone__c,Prospect_Client__r.Email__c,Prospect_Client__r.ShippingStreet,Prospect_Client__r.Name,Prospect_Client_City__c,Name,Id From Invoice__c i where id =:invId];
if(invObj !=null) {
paymentObj.invoice_numbers__c = invObj.Name;
paymentObj.Invoice_Number__c = invObj.id;
paymentObj.City__c=invObj.Prospect_Client_City__c;
paymentObj.State__c=invObj.Prospect_Client__r.ShippingState;
paymentObj.Zip__c=invObj.Prospect_Client__r.ShippingPostalCode;
paymentObj.Name__c=invObj.Prospect_Client__r.Name;
paymentObj.Street__c=invObj.Prospect_Client__r.ShippingStreet;
paymentObj.Phone__c=invObj.Prospect_Client__r.Cell_Phone__c;
paymentObj.Email_Address__c=invObj.Prospect_Client__r.Email__c;
}
}
}
}
public Pagereference save(){
if(paymentObj.Amount__c==null)
{
ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'please enter Amount and insert.');
ApexPages.addMessage(myMsg);
}
upsert paymentObj;
// ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM,'Payment Created Successfully.Thank you!'));
Pagereference refpyob = new Pagereference('/apex/Invoice?id='+paymentObj.Invoice_Number__c);
refpyob.setRedirect(true);
return refpyob;
}
}
test class i am getting 84 percent
@isTest
public with sharing class TestAddPaymentController{
public static testMethod void unitTestInvoice7()
{
try{
// String strRetUrl = ApexPages.CurrentPage().getParameters().put('retURL','/apex/AddPaymentVFPage');
User objUser =[select Id, Name ,Email from User Where UserName = 'naveen@cloudprousa.com.test'];
Staff__c objStaff = new Staff__c(User__c = objUser.Id,Name='Naveen1',Last_Name__c='Alasapuri1');
insert objStaff;
Account objAcc = new Account(Name = 'Test Prospect4', OwnerId=objUser.Id, Company_Name__c='test',Sales_Consultant__c = objStaff.Id,
Cell_Phone__c='987', ShippingStreet='test', ShippingCity='test', ShippingState='NJ');
insert objAcc;
Lead_Source__c objLS = new Lead_Source__c ( Name = 'Test Lead',Sales_Consultant__c =objStaff.Id);
insert objLS;
//SalesWorksheet record insertion
Salesworksheet__c objSW = new Salesworksheet__c(C_Adjustment__c = 10,Lead_Source__c=objLS.Id);
insert objSW;
//User objUser =[select Id from User Where UserName = 'naveen@cloudprousa.com.test'];
//Staff__c objStaff = new Staff__c(User__c = objUser.Id);
//insert objStaff;
Invoice__c objInv = new Invoice__c(Sales_Worksheet__c = objSW.Id);
insert objInv;
objSW.Technician__c = objStaff.Id;
update objSW;
//Payment record insertion
Payment__c objPayment= new Payment__c(Amount__c=1000);
insert objPayment;
// insert objInv;
String strInvId = ApexPages.CurrentPage().getParameters().put('invid',objInv.id);
ApexPages.StandardController controller = new ApexPages.StandardController(objPayment);
AddPaymentController objCGC = new AddPaymentController(controller);
objCGC.save();
//ApexPages.currentPage().getParameters().put('invid',objInv.id);
// ApexPages.Message[] pageMessages = ApexPages.getMessages();
// System.assertNotEquals(0, pageMessages.size());
// PageReference pageRef =Page.Invoice;
// Test.setCurrentPageReference(pageRef);
// ApexPages.CurrentPage().getParameters().put('retURL','/apex/Invoice?id='+objInv.Id);
}catch(exception e)
{
system.debug('Exception..'+e);
}
}
public static testMethod void unitTestInvoice8()
{
try{
// String strRetUrl = ApexPages.CurrentPage().getParameters().put('retURL','/apex/AddPaymentVFPage');
User objUser =[select Id, Name ,Email from User Where UserName = 'naveen@cloudprousa.com.test'];
Staff__c objStaff = new Staff__c(User__c = objUser.Id,Name='Naveen1',Last_Name__c='Alasapuri1');
insert objStaff;
Account objAcc = new Account(Name = 'Test Prospect4', OwnerId=objUser.Id, Company_Name__c='test',Sales_Consultant__c = objStaff.Id,
Cell_Phone__c='987', ShippingStreet='test', ShippingCity='test', ShippingState='NJ');
insert objAcc;
Lead_Source__c objLS = new Lead_Source__c ( Name = 'Test Lead',Sales_Consultant__c =objStaff.Id);
insert objLS;
//SalesWorksheet record insertion
Salesworksheet__c objSW = new Salesworksheet__c(C_Adjustment__c = 10,Lead_Source__c=objLS.Id);
insert objSW;
//User objUser =[select Id from User Where UserName = 'naveen@cloudprousa.com.test'];
//Staff__c objStaff = new Staff__c(User__c = objUser.Id);
//insert objStaff;
Invoice__c objInv = new Invoice__c(Sales_Worksheet__c = objSW.Id);
insert objInv;
objSW.Technician__c = objStaff.Id;
update objSW;
//Payment record insertion
Payment__c objPayment= new Payment__c(Amount__c=1000);
insert objPayment;
// insert objInv;
// String strInvId = ApexPages.CurrentPage().getParameters().put('invid',objInv.id);
ApexPages.StandardController controller = new ApexPages.StandardController(objPayment);
AddPaymentController objCGC = new AddPaymentController(controller);
objCGC.save();
PageReference testPage = new pagereference('/apex/Invoice?id='+objInv.Id);
//change the following to opp.id instead of 'opp.id'
// testPage.getParameters().put('invid',objInv.id);
Test.setCurrentPage(testPage);
}catch(exception e)
{
system.debug('Exception..'+e);
}
}
}
public class AddPaymentController {
public Payment__C paymentObj {get;set;}
Public String invId {get;set;}
public AddPaymentController(ApexPages.StandardController controller) {
if(paymentObj == null) {
paymentObj = new Payment__C();
String invId = ApexPages.CurrentPage().getParameters().get('invid');
if(invId !=null) {
Invoice__c invObj = [Select Prospect_Client__r.ShippingPostalCode,Prospect_Client__r.ShippingState,Prospect_Client__r.Cell_Phone__c,Prospect_Client__r.Email__c,Prospect_Client__r.ShippingStreet,Prospect_Client__r.Name,Prospect_Client_City__c,Name,Id From Invoice__c i where id =:invId];
if(invObj !=null) {
paymentObj.invoice_numbers__c = invObj.Name;
paymentObj.Invoice_Number__c = invObj.id;
paymentObj.City__c=invObj.Prospect_Client_City__c;
paymentObj.State__c=invObj.Prospect_Client__r.ShippingState;
paymentObj.Zip__c=invObj.Prospect_Client__r.ShippingPostalCode;
paymentObj.Name__c=invObj.Prospect_Client__r.Name;
paymentObj.Street__c=invObj.Prospect_Client__r.ShippingStreet;
paymentObj.Phone__c=invObj.Prospect_Client__r.Cell_Phone__c;
paymentObj.Email_Address__c=invObj.Prospect_Client__r.Email__c;
}
}
}
}
public Pagereference save(){
if(paymentObj.Amount__c==null)
{
ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'please enter Amount and insert.');
ApexPages.addMessage(myMsg);
}
upsert paymentObj;
// ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM,'Payment Created Successfully.Thank you!'));
Pagereference refpyob = new Pagereference('/apex/Invoice?id='+paymentObj.Invoice_Number__c);
refpyob.setRedirect(true);
return refpyob;
}
}
test class i am getting 84 percent
@isTest
public with sharing class TestAddPaymentController{
public static testMethod void unitTestInvoice7()
{
try{
// String strRetUrl = ApexPages.CurrentPage().getParameters().put('retURL','/apex/AddPaymentVFPage');
User objUser =[select Id, Name ,Email from User Where UserName = 'naveen@cloudprousa.com.test'];
Staff__c objStaff = new Staff__c(User__c = objUser.Id,Name='Naveen1',Last_Name__c='Alasapuri1');
insert objStaff;
Account objAcc = new Account(Name = 'Test Prospect4', OwnerId=objUser.Id, Company_Name__c='test',Sales_Consultant__c = objStaff.Id,
Cell_Phone__c='987', ShippingStreet='test', ShippingCity='test', ShippingState='NJ');
insert objAcc;
Lead_Source__c objLS = new Lead_Source__c ( Name = 'Test Lead',Sales_Consultant__c =objStaff.Id);
insert objLS;
//SalesWorksheet record insertion
Salesworksheet__c objSW = new Salesworksheet__c(C_Adjustment__c = 10,Lead_Source__c=objLS.Id);
insert objSW;
//User objUser =[select Id from User Where UserName = 'naveen@cloudprousa.com.test'];
//Staff__c objStaff = new Staff__c(User__c = objUser.Id);
//insert objStaff;
Invoice__c objInv = new Invoice__c(Sales_Worksheet__c = objSW.Id);
insert objInv;
objSW.Technician__c = objStaff.Id;
update objSW;
//Payment record insertion
Payment__c objPayment= new Payment__c(Amount__c=1000);
insert objPayment;
// insert objInv;
String strInvId = ApexPages.CurrentPage().getParameters().put('invid',objInv.id);
ApexPages.StandardController controller = new ApexPages.StandardController(objPayment);
AddPaymentController objCGC = new AddPaymentController(controller);
objCGC.save();
//ApexPages.currentPage().getParameters().put('invid',objInv.id);
// ApexPages.Message[] pageMessages = ApexPages.getMessages();
// System.assertNotEquals(0, pageMessages.size());
// PageReference pageRef =Page.Invoice;
// Test.setCurrentPageReference(pageRef);
// ApexPages.CurrentPage().getParameters().put('retURL','/apex/Invoice?id='+objInv.Id);
}catch(exception e)
{
system.debug('Exception..'+e);
}
}
public static testMethod void unitTestInvoice8()
{
try{
// String strRetUrl = ApexPages.CurrentPage().getParameters().put('retURL','/apex/AddPaymentVFPage');
User objUser =[select Id, Name ,Email from User Where UserName = 'naveen@cloudprousa.com.test'];
Staff__c objStaff = new Staff__c(User__c = objUser.Id,Name='Naveen1',Last_Name__c='Alasapuri1');
insert objStaff;
Account objAcc = new Account(Name = 'Test Prospect4', OwnerId=objUser.Id, Company_Name__c='test',Sales_Consultant__c = objStaff.Id,
Cell_Phone__c='987', ShippingStreet='test', ShippingCity='test', ShippingState='NJ');
insert objAcc;
Lead_Source__c objLS = new Lead_Source__c ( Name = 'Test Lead',Sales_Consultant__c =objStaff.Id);
insert objLS;
//SalesWorksheet record insertion
Salesworksheet__c objSW = new Salesworksheet__c(C_Adjustment__c = 10,Lead_Source__c=objLS.Id);
insert objSW;
//User objUser =[select Id from User Where UserName = 'naveen@cloudprousa.com.test'];
//Staff__c objStaff = new Staff__c(User__c = objUser.Id);
//insert objStaff;
Invoice__c objInv = new Invoice__c(Sales_Worksheet__c = objSW.Id);
insert objInv;
objSW.Technician__c = objStaff.Id;
update objSW;
//Payment record insertion
Payment__c objPayment= new Payment__c(Amount__c=1000);
insert objPayment;
// insert objInv;
// String strInvId = ApexPages.CurrentPage().getParameters().put('invid',objInv.id);
ApexPages.StandardController controller = new ApexPages.StandardController(objPayment);
AddPaymentController objCGC = new AddPaymentController(controller);
objCGC.save();
PageReference testPage = new pagereference('/apex/Invoice?id='+objInv.Id);
//change the following to opp.id instead of 'opp.id'
// testPage.getParameters().put('invid',objInv.id);
Test.setCurrentPage(testPage);
}catch(exception e)
{
system.debug('Exception..'+e);
}
}
}
Full code
Please check below blog for more information in test classess:-
http://amitsalesforce.blogspot.in/2015/06/best-practice-for-test-classes-sample.html
Please let us know if this will help you.
Thanks,
Amit Chaudhary
All Answers
Full code
Please check below blog for more information in test classess:-
http://amitsalesforce.blogspot.in/2015/06/best-practice-for-test-classes-sample.html
Please let us know if this will help you.
Thanks,
Amit Chaudhary
Public String invId {get;set;}
public class surveypageListViewApex {
public List<Survey_Mcd__c> smc{get; set;}
public surveypageListViewApex() {
searchPOAgain();
}
public PageReference searchPOAgain(){
smc = [SELECT Name, Id, Question__c, Comment__c, Yes_No__c, X5_Scale_Rating__c
FROM Survey_Mcd__c ORDER BY CreatedDate DESC];
return null;
}
public PageReference new1(){
PageReference pg1 = Page.SurveyQuestionsPageMcd;
pg1.setRedirect(TRUE);
return pg1;
}
}