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

I need test case for this class
Hello,
Please help me to write test case for the below class.
public with sharing class ABC{
Public Integer noOfRecords{get; set;}
Public Integer size{get;set;}
public string errormsg{get;set;}
public ApexPages.StandardSetController setCon {
get{ if(setCon == null){
String status='';
String contid='';
try {
status = ApexPages.currentPage().getParameters().get('status');
status = status.trim();
contid=ApexPages.currentPage().getParameters().get('Id');
contid = contid.trim();
} catch (System.StringException e) {
System.debug('Error in param Processing: ' + e);
}
size = 10;
string queryString = string queryString = 'SELECT Id, Name FROM Sample__c Order by Name WHERE InquiryContent__c = \''+contid+ '\' AND Status__c = \''+status +'\' Order by Name';
setCon = new ApexPages.StandardSetController(Database.getQueryLocator(queryString));
setCon.setPageSize(size);
noOfRecords = setCon.getResultSize();
if(noOfRecords ==0) {
errormsg='No records to display';
} else {
errormsg='';
}
}
return setCon;
}set;
}
Public List<Sample__c> sampleList(){
List<Sample__c> inqList = new List<Sample__c>();
for(Sample__c a : (List<Sample__c>)setCon.getRecords())
inqList.add(a);
return inqList;
}
public pageReference refresh() {
setCon = null;
sampleList();
setCon.setPageNumber(1);
return null;
}
public Integer getpage{
get{ integer i;
if(math.mod(noOfRecords,size)==0) {
i=noOfRecords/size;
}else {
i=noOfRecords/size+1;
}
return i;
}
set;}
}
This is ur problem.
You are passing both parameters wrongly as strings...
The first parameter needs to be a valid status enclosed in strings. The second parameter needs to be a valid ID not enclosed in strings...
My suggestion is run this SOQL query separately and make note of the status and ids. Then pass those values here correctly.
All Answers
First try to get 100% code coverage and then also test functionality.
This shud get you started....
Calvin,
While executing this i get 36% code coverage.
Please advice me..
Regards,
Cherma
The Error Message is:
Class.ABC.__sfdc_setCon: line 22, column 1 Class.ABC.
sampleList: line 36, column 1 Class.
ABC.testPagingABC: line 64, column 1
line: 22 setCon = new ApexPages.StandardSetController(Database.getQueryLocator(queryString));
Line:36 for(Sample__c a : (List<Sample__c>)setCon.getRecords())
Line:64 Si.sampleList();
Hi That was just sample code to get you started. If you click the test coverage it will tell which lines are no tested and you can add statements in ur test method to make sure those statements are invoked by your test cases.
Looks like setCon's get method expects parameters from the VF page.
You need to set those parameters in ur test case before calling the methods of the class.
Create a page reference and set it as the current page.
Then you need to set your parameters correctly based on their data types.
If my answer helped you, please mark it as the solution so others can also benefit.
Calvin,
Thanks,Now reach 48% with error. Here is my test case..Please help me..
static testmethod void testABC()
{
ABC Si=new ABC ();
Test.startTest();
PageReference pageRef = Page.MyPage;
Test.setCurrentPage(pageRef);
ApexPages.currentPage().getParameters().put('status ', 'contid');
String status='';
String contid='';
integer noOfRecords;
ApexPages.StandardSetController setCon;
string queryString = 'SELECT Id, Name FROM Sample__c Order by Name WHERE InquiryContent__c = \''+contid+ '\' AND Status__c = \''+status +'\' Order by Name';';
integer size=10;
try{
setCon = new ApexPages.StandardSetController(Database.getQueryLocator(queryString));
Setcon.setPageSize(size);
if(noOfRecords ==0) {
system.debug('No records to display');
} else {
}
}
Catch (Exception e){}
System.assertNotEquals(Si, null);
Si.sampleList();
Si.refresh();
// Integer i = Si.getpage();
test.stopTest();
}
Error Message:
Class.ABC.__sfdc_setCon: line 20, column 1
Class.ABC.sampleList: line 34, column 1
Class.ABC.testABC: line 89, column 1
The Line 20 is : setCon = new ApexPages.StandardSetController(Database.getQueryLocator(queryString));
Line 34 is: for(Sample__c a : (List<Sample__c>)setCon.getRecords())
The problem is the query in test case is doesn't execute..How can i rectify this error..please suggest me..
Thanks.
This code reach 56%. Think if the query works fine i will get above 75%.. Please help me..
static testmethod void testABC()
{
ABCSi=new ABC();
ApexPages.StandardSetController setCon;
try{
Test.startTest();
System.assertNotEquals(Si, null);
ApexPages.currentPage().getParameters().put('status ','complete');
ApexPages.currentPage().getParameters().put('contid', 'a0hQ0000002yfkK');
//String status='complete';
//String contid='a0hQ0000002yfkK';
String queryString;
integer noOfRecords;
integer size=10;
String queryString = 'SELECT Id, Name,InquiryContent__c,Status__c,ModifyDate__c '+
' FROM Sample__c WHERE InquiryContent__c = \''+contid+
'\' AND Status__c = \''+status +'\' Order by Name';
try{
setCon = new ApexPages.StandardSetController(Database.getQueryLocator(queryString));
Setcon.setPageSize(size);
if(noOfRecords ==0) {
system.debug('No records to display');
} else {
//errormsg='';
}
}
Catch (Exception e){}
List<sample__c> inqList = new List<sample__c>();
sample__c ob =new sample__c;
inqList.add(ob);
System.assert(inqList.size()>0);
Si.refresh();
Si.Samplelist();
try
{
Integer i = Si.getpage;
System.assertNotEquals(Si, null);
if(math.mod(noOfRecords,size)==0) {
i=noOfRecords/size;
}else {
i=noOfRecords/size+1;
}
}
catch(Exception E){}
test.stopTest();
}
catch(Exception E){}
}
}
This is ur problem.
You are passing both parameters wrongly as strings...
The first parameter needs to be a valid status enclosed in strings. The second parameter needs to be a valid ID not enclosed in strings...
My suggestion is run this SOQL query separately and make note of the status and ids. Then pass those values here correctly.
thanks Calvin..now i got 88 %
No problem. Glad it helped.