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
sunny@99-chgsunny@99-chg 

Need Test Coverage Help

Hai All,

 

I need Some suggestion to write test coverage for the below code

 

in my page i wrote component as 

 

<apex:inputText value="{!ESNnumber}" style="width:100px;" id="ESNnumber" >
<c:autocomplete autocomplete_textbox="{!$Component.ESNnumber}" objectname="o2bc__Item_Serial__c" parentName="{!$CurrentPage.parameters.PhoneId}" />
</apex:inputText>

Here is the Component for Auto search functionality

 

<apex:component controller="autoCompleteController">

 

<!-- JQuery Files -->

<apex:includeScript value="{!URLFOR($Resource.jqueryui18, 'js/jquery-1.8.0.min.js')}" />

<apex:includeScript value="{!URLFOR($Resource.jqueryui18, 'js/jquery-ui-1.8.23.custom.min.js')}" />

<apex:stylesheet value="{!URLFOR($Resource.jqueryui18,'css/ui-lightness/jquery-ui-1.8.23.custom.css')}"/>

<!-- Attributes Required For Component -->
<apex:attribute name="objectname" description="The object name you want to look for." type="String" required="true"/>
<apex:attribute name="ParentName" description="The object name you want to look for." type="String" required="false"/>
<apex:attribute name="additionalfield" description="Any additional fields you'd like to search and include in the display." type="String" required="false"/>
<apex:attribute name="profilename" description="To filter on the basis of profile name and include in the display." type="String" required="false"/>
<apex:attribute name="autocomplete_textbox" description="The ID for the Autocomplete List Textbox." type="String" required="true"/>

<!-- CSS Style -->
<style>
.ui-autocomplete-loading {background: white url({!$Resource.loadingIcon}) right center no-repeat;}
</style>

<!-- Javascript -->
<script type="text/javascript">
var j$ = jQuery.noConflict();
j$(document).ready(function()
{

var sObjects;
var queryTerm;

j$(esc('{!autocomplete_textbox}')).autocomplete({
minLength: 1,
source: function(request, response) {
queryTerm = request.term;
autoCompleteController.findSObjects("{!objectname}", request.term, "{!additionalfield}","{!ParentName}","{!profilename}", function(result, event){
if(event.type == 'exception')
{
alert(event.message);
} else
{
sObjects = result;
response(sObjects);
}
});
},
focus: function( event, ui ) {
j$(esc('{!autocomplete_textbox}')).val( ui.item.Name );
return false;
},
select: function( event, ui ) {
j$(esc('{!autocomplete_textbox}')).val( ui.item.Name );
j$(esc('{!autocomplete_textbox}_lkid')).val( ui.item.Id );
j$(esc('{!autocomplete_textbox}_lkold')).val( ui.item.Name );
if (event.keyCode == 13) {
event.preventDefault();
}
return false;
},
})
.data( "autocomplete" )._renderItem = function( ul, item ) {
var entry = item.Name;
if("{!additionalfield}" !='')
{
j$.each("{!additionalfield}".split(",") , function(key, value) {
entry = entry + " " + item[value];
});
}
//entry = entry + "</a>";
//entry = entry.replace(queryTerm, "<b>" + queryTerm + "</b>");
entry = entry.replace( new RegExp( "(" + queryTerm + ")" , "gi" ), "<strong>$1</strong>" );
return j$( "<li></li>" )
.data( "item.autocomplete", item )
.append( "<a>" + entry + "</a>")
.appendTo( ul );
};
});

function esc(myid)
{
return '#' + myid.replace(/(:|\.)/g,'\\\\$1');
}

</script>
</apex:component>

 

==================================================

 

Here is my Ccontroller for the above component

 

global class autoCompleteController
{
@RemoteAction
global static SObject[] findSObjects(string obj, string qry, string addFields,string parent,string profilename)
{
/* More than one field can be passed in the addFields parameter
Split it into an array for later use */
List<String> fieldList=new List<String>();
if (addFields != '')
fieldList = addFields.split(',');
Attachment oAttachment = [SELECT ContentType,Id,ParentId,Parent.Name FROM Attachment WHERE ContentType LIKE '%image%' AND Id =:Parent];
o2bc__Item__c oItem =[select Id,o2bc__Sell_Price__c,Qty__c from o2bc__Item__c where ID =:oAttachment.ParentId];

/* Check whether the object passed is valid */
Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe();
Schema.SObjectType sot = gd.get(obj);
if (sot == null)
{
return null;
}

/* Creating the filter text */
String filter = ' like \'' + String.escapeSingleQuotes(qry) + '%\'';


/* Begin building the dynamic soql query */
String soql = 'SELECT Name';

/* If any additional field was passed, adding it to the soql */
if (fieldList.size()>0)
{
for (String s : fieldList)
{
soql += ', ' + s;
}
}

/* Adding the object and filter by name to the soql */
soql += ' from ' + obj + ' where name' + filter;
string pl='new';

if(profilename!='')
{
//profile name and the System Administrator are allowed
soql += ' and Profile.Name like \'%' + String.escapeSingleQuotes(profilename) + '%\'';
system.debug('Profile:'+profilename+' and SOQL:'+soql);
}
if(Parent!='')
{
soql += ' and o2bc__Item__c =\''+oItem .id+'\'';
soql += 'and o2bc__Status__c=\''+Pl+'\' ';
}
/* Adding the filter for additional fields to the soql */
/* if (fieldList != null)
{
for (String s : fieldList)
{
soql += ' or ' + s + filter;
}
} */

soql += ' order by Name limit 20';

system.debug('Qry: '+soql);

List<sObject> L = new List<sObject>();
try
{
L = Database.query(soql);
}
catch (QueryException e)
{
system.debug('Query Exception:'+e.getMessage());
return null;
}
return L;
}
}

 

Then Anyone Suggest me How to write test Coverage for the above controller.

HariDineshHariDinesh

Hi,

 

Your apex code is normal code it seems, you will not feel difficult in writing test class for this.

 

Just go through below links which will guide in proper way to write test methods.

You can understand those very easily as you have knowledge on Coding.

 

http://wiki.developerforce.com/page/An_Introduction_to_Apex_Code_Test_Method

 

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_test.htm

 

For your understanding i am giving skeliton of Test class.

 

@isTest
 private class testclassname
  {
    static testmethod void testmethodname()
     {
       // Data prperation    

          Book__c books = new Book__c (Name='Behind the Cloud', Price__c=100);
          insert books;
 
      // calling main class

         mainclassname cc - new mainclassname();

         cc.methodname(required parameters);  // requird parameters will get from above data

         }
   // another method if neede
   }