+ 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();
        isboolean=true;
        booleans=false;
    }
    public void Accept(){
        try {  
            INSERT booking;
            isboolean=false;
            booleans=true;
        }
        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:

http://amitsalesforce.blogspot.com/2015/06/best-practice-for-test-classes-sample.html

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
www.ajaydubedi.com 
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

@isTest 
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;
        
        Test.StartTest(); 
        
        PageReference pageRef = Page.BookingController; // Add your VF page Name here
        pageRef.getParameters().put('id', String.valueOf(testBooking_Link__c.Id));
        Test.setCurrentPage(pageRef);
        
        myController testAccPlan = new myController();   
        
        //testAccPlan.save(); call all your function here
        Test.StopTest();
    }
}

Errors in Bold in code

Help me out in thiss..


Thanks and Regards,
Azar Khasim.