You need to sign in to do that
Don't have an account?
Michael 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:
Test Class:
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); } }
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
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.