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
Balu_SFDCBalu_SFDC 

100% code coverage of Test Method For Apex Class

Hi everyone,,

 

I am trying write the test method for the follwing apex class..

i am get 28% of code coverage..please help in this......for getting more than 75% code coverage..

.

My apex class is::

public class search {

public List<student__c> students{get;set;}
public List<student__c> getstudents(){
return students;
}
public student__c getstudent(){
Id id=system.currentpagereference().getparameters().get('id');
return id == null ? new student__c() : [SELECT Id, Name
FROM Student__c
WHERE Id = :id];
}
public String studentname { get; set; }

public String studentId { get; set; }

public PageReference search() {


if(studentId.length()>0)
{
string stdid='select id, name,Student_Name__c,Email_ID__c,Phone_Number__c from student__C where name=:studentId';
students=Database.Query(stdid);
if(students.size()==0)
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Student Id Not Found'));
return null;
}
if(studentname.length()>0)
{
string stdname='select id,name,Student_Name__c,Email_ID__c,Phone_Number__c from student__C where student_name__C LIKE \'' + Studentname+ '%\'';
students=Database.Query(stdname);
if(students.size()==0)
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Student Name Not Found'));
return null;
}
if((studentId.length()>0)&&(studentname.length()>0))
{
string std='select id,name,Student_Name__c from student__C,Email_ID__c,Phone_Number__c where name=:studentId';
students=Database.Query(std);
return null;
}
else
{
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Please Enter Any Value'));
return null;
}
}
}

 

Test Class

 

@istest
public class testMyPage {
static testMethod void myPage_Test()
{
//Test converage for the myPage visualforce page
PageReference pageRef = Page.search_student;
Test.setCurrentPageReference(pageRef);
// create an instance of the controller
search myPageCon = new search();
//try calling methods/properties of the controller in all possible scenarios
// to get the best coverage.
List<student__c> std = myPageCon.getstudents();
//test when type == null
myPageCon.search();
mypagecon.studentname='b';
myPageCon.getstudents();
}
}

thanks in advance........

 

Regards.

Balu

liron169liron169
I didn't get into all your code, but one of the issue that preventing you to cover is that in test class you don't have data at the tables at first - all the tables are empty and you must insert before starting the real tests.

You need to add in the test class, in the start:

List<student__C> studentLst=new List<student__C>();

//add some students to the list

insert studentList;

//starting test ( now you have students in the table)
Balu_SFDCBalu_SFDC

hi,,

thanks for your reply..

 

i am just searching the records in the Objects and the displying in the VF Page...

 this is my VF Page

<apex:page controller="search" sidebar="false" tabStyle="search_student__tab">

<apex:sectionHeader title="Search Students"/>
<apex:form >
<script type='text/javascript'>
function noenter(ev) {
if (window.event && window.event.keyCode == 13 || ev.which == 13) {
doSearchAF();
return false;
} else {
return true;
}
}
</script>

<apex:pageBlock title="Search Students Here" onkeypress="return noenter(event)">
<apex:image url="https://ap1.salesforce.com/resource/1350637855000/search" width="90px" height="75px"/>
<center>
<apex:pageblockSection title="Search By Student Id" >
<center>
<apex:inputtext value="{!studentId}" onkeypress="return noenter(event)"/>
</center>
</apex:pageblockSection>
<apex:pageblocksection title="Search by Name">
<center><apex:inputtext value="{!studentname}" onkeypress="return noenter(event)"/>
</center>
</apex:pageblocksection>
<apex:commandButton value="Search" action="{!search}" reRender="pgb,pmsgs"/>
</center>
<apex:pagemessages id="pmsgs"/>
<apex:pageblocktable value="{!students}" var="s" id="pgb">
<apex:column >
<apex:commandLink reRender="detail">{!s.name}
<apex:param name="id" value="{!s.id}"/>
</apex:commandLink>

</apex:column>
<apex:column value="{!s.Student_Name__c}"/>
<apex:column value="{!s.Email_ID__c}"/>
<apex:column value="{!s.Phone_Number__c}"/>
</apex:pageblocktable>
<apex:actionFunction name="doSearchAF" action="{!Search}" />
</apex:pageBlock>
</apex:form>
<apex:outputpanel id="detail">
<apex:detail subject="{!student}" title="false" relatedList="false"/>
</apex:outputpanel>

</apex:page>

 

Regards 

Balu

amidstcloudamidstcloud

Hi Balu.

 

Please insert a record with all the suitable fields and value .   Please make when you call the search func from the test class You should get the some Id in the variable in the following query 

 

string stdid='select id, name,Student_Name__c,Email_ID__c,Phone_Number__c from student__C where name=:studentId';

 

 

Balu_SFDCBalu_SFDC

hi ankit,

 

Thanks for your reply...

i am just seraching the recoards in the Objects and displying in the Vf Page

in my test method i already call these apex class and VF page..

and 

i am already calling the search()method in the test method so i don't think to call the SOQL in the test method..

 

Regards 

Balu

liron169liron169
If you think it's not needed, then run the test from SF and then check which lines are not covered.
In SF go to the class and click on the field code coverage of the class.

This will help you to identify what you're missing.