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
Mayank RanaMayank Rana 

having trouble writing a test class for a custom controller used in an VF page. I don't know where the problem occurs,, I'm posting my Contoller class below. Please help me,,If anybody finds where I went wrong...

public class BikeController
{

    public BikeController() {}

    public Sell_Your_Bike__c bike{set;get;}
 
    public BikeController(ApexPages.StandardController std)
    {
        this.bike=(Sell_Your_Bike__c)std.getRecord();
    }
    
    public Sell_Your_Bike__c saveRecord()
    {
        Sell_Your_Bike__c bike=new Sell_Your_Bike__c
        (
            Registration_Number__c=bike.Registration_Number__c,
            Owner_Name__c=bike.Owner_Name__c,
            Model__c=bike.Model__c,
            Mileage__c=bike.Mileage__c,
            Cubic_Capicity__c=bike.Cubic_Capicity__c,
            Fuel_Capacity__c=bike.Fuel_Capacity__c,
            Speedometer__c=bike.Speedometer__c,
            Disk_Brake__c=bike.Disk_Brake__c,
            Tubeless_Tyres__c=bike.Tubeless_Tyres__c,
            Alloys__c=bike.Alloys__c,
            Price__c=bike.Price__c,
            Contact_Number__c=bike.Contact_Number__c
        );
        
        Insert bike;
        return bike;
    }
    
    public PageReference save()
    {    
        Sell_Your_Bike__c bike= this.saveRecord();
        PageReference pg=null;
        if(!bike.Id.equals(NULL))
        {
            pg=new PageReference('/apex/Bike_SuccessPage?ID='+bike.Id+'&NAME='+bike.Owner_Name__c);
            pg.setRedirect(true);
            return pg;
        }
        else
        {
            pg=Page.Bike_FailurePage;
            pg.setRedirect(true);
            return pg;
        }
    }
    
    public PageReference save_new()
    {
        Sell_Your_Bike__c bike= this.saveRecord();
        PageReference pg=null;
        if(!bike.Id.equals(NULL))
        {
            pg=new PageReference('/apex/VFP_BikeSell');
            pg.setRedirect(true);
        }
        return pg;        
    }
    
    public PageReference cancel()
    {
        PageReference pg=new PageReference('/apex/VFP_BikeSell');
        pg.setRedirect(true);
        return pg;
    }
    
}
Harish M 8Harish M 8
In test class please create a record of Sell_Your_Bike__c
Sell_Your_Bike__c s=new Sell_Your_Bike__c();
s.Registration_Number__c='1234';
-----
----
----
s.Contact_Number__c='99999999';

then
ApexPages.StandardController controller=new ApexPages.StandardController(s);    //Here send your record to page controller
BikeController c=new BikeController(controller);

then call other methods also like
c.save();
---
---
Try this
Harish M 8Harish M 8
@Mayank Rana i think if you write like above, it almost cover more than 80% of code coverage..
Mayank RanaMayank Rana
Thanks Mr. Harish,, For resolving my Query..... I got my TestClass with 91% code Coveraged..
Here's what my TestClass...


@isTest
public class BikeController_Test
{
    public static TestMethod void Test1()
    {
        Sell_Your_Bike__c bike=new Sell_Your_Bike__c
            (
                Registration_Number__c='UK0876443',
                Owner_Name__c='RajKumar',
                Model__c='Honda',
                Mileage__c=45.0,
                Cubic_Capicity__c=150.0,
                Fuel_Capacity__c=8.0,
                Speedometer__c='Analog',
                Disk_Brake__c=True,
                Tubeless_Tyres__c=False,
                Alloys__c=True,
                Price__c=35000.0,
                Contact_Number__c='9759776545'
            );
        
        ApexPages.StandardController controller=new ApexPages.StandardController(bike);
        BikeController bkk=new BikeController();
        BikeController bk=new BikeController(controller);
        bk.save();
        bk.save_new();
        bk.cancel();
    }

}