You need to sign in to do that
Don't have an account?
swain 10
Hello Can anyone please suggest how to write a test class for the below class. I am new to test classes please help.
public class SaveAndNew
{
ApexPages.StandardController con;
public String queryString {get;set;}
public SaveAndNew(ApexPages.StandardController controller)
{
con = controller;
PageReference thisPage = ApexPages.currentPage();
List<String> url = thisPage.getUrl().split('\\?');
queryString = url[1];
System.debug('----->>'+queryString);
}
public PageReference saveNew()
{
try
{
con.save();
Schema.DescribeSObjectResult describeResult = con.getRecord().getSObjectType().getDescribe();
PageReference pr = new PageReference('/' + describeResult.getKeyPrefix() + '/e?'+ queryString);
pr.setRedirect(true);
return pr;
}catch(System.DMLException e) {
return null;
}
}
}
{
ApexPages.StandardController con;
public String queryString {get;set;}
public SaveAndNew(ApexPages.StandardController controller)
{
con = controller;
PageReference thisPage = ApexPages.currentPage();
List<String> url = thisPage.getUrl().split('\\?');
queryString = url[1];
System.debug('----->>'+queryString);
}
public PageReference saveNew()
{
try
{
con.save();
Schema.DescribeSObjectResult describeResult = con.getRecord().getSObjectType().getDescribe();
PageReference pr = new PageReference('/' + describeResult.getKeyPrefix() + '/e?'+ queryString);
pr.setRedirect(true);
return pr;
}catch(System.DMLException e) {
return null;
}
}
}
I followed your process still no coverage.
@isTest Private class SaveAndNewTest
{
static testMethod void saveNewTest()
{
// here I am taken account, you have to create your standardcontroller object record Expense_Line_Item__c ac = new Expense_Line_Item__c(Expense__c='EXP-00135',Amount__c=1200);
Insert ac;
ApexPages.StandardController sc = new ApexPages.StandardController(ac); PageReference pageRef= page.expenselineitemsfields; // add your page name
System.Test.setCurrentPage(pageRef);
pageRef.getParameters().put('id', 'abc\\?def');
SaveAndNew savenNew = new SaveAndNew(sc);
savenNew.saveNew();
}
}