0

I wrote test class for the Custom Controller Class.while running test class it has covered 85%. 15% of the code does not covered due to Page Reference. I'm getting how to write test class for the page reference part.

Visualforce Page:

<apex:page controller="studentInsertUpdateController" docType="html-5.0" >
<apex:form >
    <apex:pageBlock title="Insert Record">
        <apex:pageBlockSection title="Insert Student Record">

            <apex:inputtext required="true" label="Student Name"  value="{!studentName}"/><br/>
            <apex:inputtext label="Batch Name" value="{!batchName}"/><br/>
            <apex:inputtext label="Fees Amount"  value="{!studentsFees}"/><br/>         
        </apex:pageBlockSection>
       <apex:pageBlockSection title="Update Student Records">
          <apex:actionRegion >
         <apex:pageBlockTable value="{!stuList}" var="s"> 
           <apex:column >
               <apex:commandLink value="Edit" action="/{!s.Id}/e"/>
            </apex:column>
           <apex:column value="{!s.name}"/>
           <apex:column value="{!s.Batch_Name__c}"/>   
           <apex:column value="{!s.Fee__c}"/>        
         </apex:pageBlockTable>    
            </apex:actionRegion>
        </apex:pageBlockSection>
        <apex:pageBlockButtons >
            <apex:commandButton value="Save" action="{!insertStudentRecord}"/>
        </apex:pageBlockButtons>
        </apex:pageBlock>               
</apex:form>
</apex:page>

Custom Controller Class:

public class studentInsertUpdateController {
public string studentName{get;set;}
public string batchName{get;set;}
public decimal studentsFees{get;set;}
public List<Student__c> stuList{get;set;}
public studentInsertUpdateController (){
   stuList =[ select id,name,Batch_Name__c,College__c,Fee__c from student__c where Fee__c!=null order by name];
}
public pagereference insertStudentRecord(){
  student__c stuObj = new student__c();
  stuObj.name = studentName;
  stuObj.Batch_Name__c = batchName;
  stuObj.Fee__c = studentsFees;
  insert stuObj;
  pagereference ref = new pagereference('/'+stuObj.id); 
  return ref;
}
}

Test Class Code:

@isTest
public class Task2_testCase {
    public static testmethod void myTest2(){
       studentInsertUpdateController obj = new studentInsertUpdateController();
        Training__c tr= new Training__c();
        tr.Name='t1';
        tr.Fee__c=4000;
        Batch__c btch = new Batch__c();
        btch.Name='B2';
        btch.Training_Name__c=tr.id;
        obj.studentName='deepan';
        obj.batchName=btch.id;
        obj.studentsFees=4000;
        student__c stu = new Student__c();
        stu.Name='deepan';
        stu.Batch_Name__c=btch.id;
        stu.Fee__c=4546;
        PageReference pageRef = obj.insertStudentRecord();
        pageRef.getParameters().put('id',string.valueOf(stu.id));
        Test.setCurrentPage(pageRef);

    }
}

Can anyone help me to understand how to write test class for page reference. thanks in advance.

1
  • did you check your developer log or test class i think you are geetting exception in insert stuObj; line. Commented Oct 3, 2015 at 11:54

2 Answers 2

1

What lines are not covered?

Regarding the page reference, your test pattern is incorrect.

When testing a page reference you need to assert that the page reference returned is correct. In the test class you only need to set the currentPage if you are pulling parameters in your controller.

When writing a test method you need to logical think through the process that will be followed by the user on the screen, then replicate it.

Once you do that, then you need to write negative test cases, for example, what if the studentName is blank, does you code handle that, write a test for it. What if no records are returned in the instantiation, does your code handle it, write a test for it, etc.

You test should look something like this:

@isTest
private class exampleTest{


    private static testmethod void basicTest(){
        //Not really needed but your controller get a list of students on instantiation so we will give it a value
        Student__c tmp = New Student__c(Fee__c = 123, Name = 'Test');
        insert tmp;

        studentInsertUpdateController obj = new studentInsertUpdateController();
        //Assert that the list is not null
        system.assertEquals(1,obj.stuList.size());

        //Now we need to populate the value like the user would on the page
        obj.studentName = 'Test Student';
        obj.batchName = 'Test Batch';
        obj.studentFees = 22.22;

        //Now we have the controller intert the student like they pressed the button on the page
        PageReference result = obj.InsertStudentRecord();

        //Now we need to get the ID of the record just created based on the name to ensure the pagereference is correct
        Student__c[] newRecord = [Select ID From Student__C Where Name = 'Test Student'];
        //Assert there was a record found
        system.assertEquals(1,newRecord.size(),'A new student record was not created');

        //Now we need to check the pagereference
        system.assertEquals(true,result.getURL.endsWith(newRecord[0].id));

    }

}

It is also advisable: than when doing any DML in a controller that you catch any errors and present appropriate messages. If there is an issue then the user would get a cryptic message and not know what to do otherwise.

It is simply not enough to throw a bunch of code in a test class and expect it to be valuable. It may cover the lines, but it does not ensure that thing are working properly.

For example, this test class would cover the lines but actually tests nothing

@isTest
private class exampleTest{


    private static testmethod void basicTest(){
        studentInsertUpdateController obj = new studentInsertUpdateController();
        obj.InsertStudentRecord();

    }

}

And looking at your test some questions you should be asking yourself:

  1. Why am I creating a Batch__c record? It is not even used in your controller
  2. Why am I creating a Training__c record? It is not even used in your controller
  3. For 1 and 2, why am I creating them and never actually inserting them? Whats the point

The business requirements that outline what you designed should be used to actually write the test methods as well

0

It seems you are not inserting student__c in the database.

try this.

   @isTest
public class Task2_testCase {
    public static testmethod void myTest2(){
       studentInsertUpdateController obj = new studentInsertUpdateController();
        Training__c tr= new Training__c();
        tr.Name='t1';
        tr.Fee__c=4000;
        insert tr;
        Batch__c btch = new Batch__c();
        btch.Name='B2';
        btch.Training_Name__c=tr.id;
        insert btch;
        student__c stu = new Student__c();
        stu.Name='deepan';
        stu.Batch_Name__c=btch.id;
        stu.Fee__c=4546;
        insert stu;

        PageReference pageRef = obj.insertStudentRecord();
        pageRef.currentPage().getParameters().put('id',stu.id);
        Test.setCurrentPage(pageRef);

    }
}
2
  • I tried this but if i put insert stu it is covering only 42% of my code .the method insertStudentRecord() is not covered.it is highlighted in red color. Commented Oct 3, 2015 at 11:37
  • Could you please try the updated code ?
    – MIX DML
    Commented Oct 3, 2015 at 11:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .