function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
ChermaduraiChermadurai 

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;}
}

Best Answer chosen by Admin (Salesforce Developers) 
calvin_nrcalvin_nr

This is ur problem.

 

ApexPages.currentPage().getParameters().put('status ', 'contid');

 

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

calvin_nrcalvin_nr

First try to get 100% code coverage and then also test functionality.

 

This shud get you started....

 

@isTest
        public static void testMethod1() {
         {           
            ABC testObj1 = new ABC();

            test.StartTest();

           testObj1.sampleList();

           testObj1.refresh();

           Interger i = testObj1.getpage();

            test.StopTest();

        }

 

 

 

ChermaduraiChermadurai

 

Calvin,

 

While executing this i get 36code coverage.

 

Please advice me..

 

Regards,

Cherma

 

ChermaduraiChermadurai

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

ChermaduraiChermadurai

line: 22  setCon = new ApexPages.StandardSetController(Database.getQueryLocator(queryString));

 

Line:36    for(Sample__c a : (List<Sample__c>)setCon.getRecords())

 

Line:64       Si.sampleList();

 

 

calvin_nrcalvin_nr

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.

calvin_nrcalvin_nr

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.

 

PageReference pageRef = Page.MyPage;
Test.setCurrentPage(pageRef);

 Then you need to set your parameters correctly based on their data types.

ApexPages.currentPage().getParameters().put('param1', param2);

 

If my answer helped you, please mark it as the solution so others can also benefit.

 

ChermaduraiChermadurai

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

ChermaduraiChermadurai

The Line 20 is : setCon = new ApexPages.StandardSetController(Database.getQueryLocator(queryString));

Line 34 is:    for(Sample__c a : (List<Sample__c>)setCon.getRecords())

 

ChermaduraiChermadurai

The problem is the query in test case is doesn't execute..How can i rectify this error..please suggest me..

 

Thanks.

 

ChermaduraiChermadurai

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){}
}
}

calvin_nrcalvin_nr

This is ur problem.

 

ApexPages.currentPage().getParameters().put('status ', 'contid');

 

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.

This was selected as the best answer
ChermaduraiChermadurai

thanks Calvin..now i got 88 %

calvin_nrcalvin_nr

No problem. Glad it helped.