+ Start a Discussion
devfredevfre 

Need help for test class

can somebody help me for writing the test class for autocompcontroller, i have seen that class in google but i'm not sure how to write test class for that..

 

this is the link of that controller..http://verticalcode.wordpress.com/2011/02/19/salesforce-javascript-remoting-jquery-and-autocomplete/

.

i need test class for the controller which is in the above link

Best Answer chosen by Admin (Salesforce Developers) 
Navatar_DbSupNavatar_DbSup

Hi,


Try the below class with test method written inside the class:
global class autoCompleteController {
@RemoteAction
global static SObject[] findSObjects(string obj, string qry, string addFields) {
// more than one field can be passed in the addFields parameter
// split it into an array for later use
List<String> fieldList;
if (addFields != null) fieldList = addFields.split(',');
// check to see if the object passed is valid
Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe();
Schema.SObjectType sot = gd.get(obj);
if (sot == null) {
// Object name not valid
return null;
}
// create the filter text
String filter = ' like \'%' + String.escapeSingleQuotes(qry) + '%\'';
//begin building the dynamic soql query
String soql = 'select id, Name';
// if an additional field was passed in add it to the soql
if (fieldList != null) {
for (String s : fieldList) {
soql += ', ' + s;
}
}
// add the object and filter by name to the soql
soql += ' from ' + obj + ' where name' + filter;
// add the filter by additional fields to the soql
if (fieldList != null) {
for (String s : fieldList) {
soql += ' or ' + s + filter;
}
}
soql += ' order by Name limit 20';
List<sObject> L = new List<sObject>();
try {
L = Database.query(soql);
}
catch (QueryException e) {
return null;
}
return L;
}
static testmethod void Test4RemoteAction()
{
string s='account';
string query='select id,name from account';
string fields='name';
autoCompleteController.findSObjects(s,query,fields);
}
}

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

 

All Answers

Navatar_DbSupNavatar_DbSup

Hi,


Try the below class with test method written inside the class:
global class autoCompleteController {
@RemoteAction
global static SObject[] findSObjects(string obj, string qry, string addFields) {
// more than one field can be passed in the addFields parameter
// split it into an array for later use
List<String> fieldList;
if (addFields != null) fieldList = addFields.split(',');
// check to see if the object passed is valid
Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe();
Schema.SObjectType sot = gd.get(obj);
if (sot == null) {
// Object name not valid
return null;
}
// create the filter text
String filter = ' like \'%' + String.escapeSingleQuotes(qry) + '%\'';
//begin building the dynamic soql query
String soql = 'select id, Name';
// if an additional field was passed in add it to the soql
if (fieldList != null) {
for (String s : fieldList) {
soql += ', ' + s;
}
}
// add the object and filter by name to the soql
soql += ' from ' + obj + ' where name' + filter;
// add the filter by additional fields to the soql
if (fieldList != null) {
for (String s : fieldList) {
soql += ' or ' + s + filter;
}
}
soql += ' order by Name limit 20';
List<sObject> L = new List<sObject>();
try {
L = Database.query(soql);
}
catch (QueryException e) {
return null;
}
return L;
}
static testmethod void Test4RemoteAction()
{
string s='account';
string query='select id,name from account';
string fields='name';
autoCompleteController.findSObjects(s,query,fields);
}
}

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

 

This was selected as the best answer
devfredevfre

hi i'm getting error like this when i'm trying to save the test class which u sent.. the error is:

 

Method does not exist or incorrect signature: autoCompleteController.findSObjects(String, String, String)

devfredevfre

hi ankit,

 

i did some mistake so i was getting error.. Again i tried with your code and it worked great and got coverage above 90%