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
Michael SchlechtMichael Schlecht 

Test class for extension class

I am trying to create a Test class for my controller extension class.  I have a custom object "Vehicle" that has a Master-Detail relationship to "Opportunity".
With my current Test class, i have 46% code coverage but unable to increase it.  Any help is greatly appreciated!

Extension Class:
public class VehicleExtension {
     Private List<Vehicle__c> vecs;
     Private Opportunity vecopp;
       
     public VehicleExtension(){
     }
  
    public VehicleExtension(ApexPages.StandardController controller) {
        this.vecopp= (Opportunity)controller.getRecord();
    }
    public List <Vehicle__c> getVehicles()
        {
         Id id = ApexPages.currentPage().getParameters().get('id');
         vecs =    [SELECT Name, Make__c,Group__c,Year_Built__c,VIN__c,Delivery_Date__c,Payment_Status1__c,Pickup_Date__c, of_Days__c,
                        Mileage_Requested__c,Internal_Cost_Total__c,Cost_Total__c
            FROM Vehicle__c where Vehicle__c.opportunity__c =:[select Name from Opportunity where Id =:id]];
         return vecs;
        }
   
   public pageReference saveStatusChange(){
        update this.vecs;
        return null;
    }
 }



Test Class:
@isTest
public class VehicleTest{
static testMethod void test() {

    	VehicleExtension fe = new VehicleExtension();
    	List<Vehicle__C> events = fe.getVehicles();
    	fe.saveStatusChange();
    	System.assertNotEquals(null, events);
   
   
      	Opportunity ac = new Opportunity(Name ='tester', CloseDate =System.today().addDays(30),StageName ='Open Request', Posting_Date__c =System.today());
      	insert ac;
    
   		ApexPages.StandardController stdController = new ApexPages.StandardController(ac); 
      	VehicleExtension fe1 = new VehicleExtension(stdController);
       }
}
Best Answer chosen by Michael Schlecht
Tyler Mowbrey 1Tyler Mowbrey 1
You need to create your data set in your test class for the Vehicle object. After line 11/12 do Vehicle__c v = new Vehicle__c(Make__c="Ford",Group__c="A",Year_Build__c=2014,Opportunity__c=acc.Id); insert v;

This will cause your getVehicles() method to actual pull data back that you can system.assertEquals.

Also you need to add the following beofre line 14:

PageReference pageRef = Page.YOURVISUALFORCEPAGENAMEHERE;
Test.setCurrentPage(pageRef);
ApexPages.currentPage().getParameters().put('Id', acc.Id);

This will ensure that you pass an id parameter through to the controller to cover line 13 in your extension.
 

All Answers

Tyler Mowbrey 1Tyler Mowbrey 1
You need to create your data set in your test class for the Vehicle object. After line 11/12 do Vehicle__c v = new Vehicle__c(Make__c="Ford",Group__c="A",Year_Build__c=2014,Opportunity__c=acc.Id); insert v;

This will cause your getVehicles() method to actual pull data back that you can system.assertEquals.

Also you need to add the following beofre line 14:

PageReference pageRef = Page.YOURVISUALFORCEPAGENAMEHERE;
Test.setCurrentPage(pageRef);
ApexPages.currentPage().getParameters().put('Id', acc.Id);

This will ensure that you pass an id parameter through to the controller to cover line 13 in your extension.
 
This was selected as the best answer
Michael SchlechtMichael Schlecht
Thank you that worked!