+ Start a Discussion
azar khasimazar khasim 

Need help for writing a test class for the above VF Page Class Controller.

Hello Guys,

I need some help regarding writing a test class for the below controller class for a VF page.

Here is my Controller Class

public class BookingController {
    public Booking_Link__c booking {get;set;}
    public Boolean isboolean {get;set;}
     public Boolean booleans {get;set;}
    public BookingController(){
        booking = new Booking_Link__c();
    public void Accept(){
        try {  
            INSERT booking;
        catch (Exception e) {
            ApexPages.addMessages (e);
           // return null;
       // PageReference pr = new ApexPages.StandardController(booking).view();
        //return null;
    public void cancel(){
        booking = new Booking_Link__c();

Please help me out from this...,

Thanks and Regards,
Azar Khasim.
Ajay K DubediAjay K Dubedi
Hi Azar,

You can write a test class for the VF page controller in the same way that you would any other. Please read the blog in the below link to learn how to write apex test classes for VF page controller in different scenarios:


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Ajay Dubedi
azar khasimazar khasim
Hello Ajay,

Yes, the above link has helped me in it.
But can u help me in this errors which i got during the time.

My Test Controller

public class BookingControllerTest {
    static testMethod void testMethod1() 
        Booking_Link__c testBooking_Link__c = new Booking_Link__c();    --->    Invalid character in identifier: testBooking_Link__c
         testBooking_Link__c.Name='Booking Link' ;    ---->  In this object my Name field is not writable.
        insert testBooking_Link__c;
        PageReference pageRef = Page.BookingController; // Add your VF page Name here
        pageRef.getParameters().put('id', String.valueOf(testBooking_Link__c.Id));
        myController testAccPlan = new myController();   
        //testAccPlan.save(); call all your function here

Errors in Bold in code

Help me out in thiss..

Thanks and Regards,
Azar Khasim.