You need to sign in to do that
Don't have an account?

Code Coverage for Test of Controller Extension
Hi all!
Relatively new to Apex but I have the controller extension below:
public class ControllerExt { ApexPages.StandardController stdCtrl; public ControllerExt(ApexPages.StandardController std) { stdCtrl=std; } public PageReference save() { stdCtrl.save(); return null; } }
And I have been, unsuccesfully, trying to write the test code for it. My current attempt is below (saves and runs with no errors), but the code coverage is 0...any suggestions?
@isTest public class ControllerExtTest { static testMethod void myPage_Test() { PageReference pageRef = Page.TypeA; Test.setCurrentPage(pageRef); Account acc1 = new Account (Name = 'Account 1', Type = 'Type A'); insert acc1; Opportunity opp1 = new Opportunity(Name = 'Opportunity1', AccountID = acc1.id, Probability = 20.00, CloseDate = Date.newInstance(System.now().year(),10,10), StageName = 'Prospect'); insert opp1; ApexPages.StandardController sc = new ApexPages.standardController(opp1); String nextPage = sc.save().getUrl(); String str1 = string.valueof (opp1.Id).substring (0, 15); System.assertEquals('/'+str1, nextPage); } }
Try this
@isTest
public class ControllerExtTest {
static testMethod void myPage_Test() {
PageReference pageRef = Page.TypeA;
Test.setCurrentPage(pageRef);
Account acc1 = new Account (Name = 'Account 1', Type = 'Type A');
insert acc1;
Opportunity opp1 = new Opportunity(Name = 'Opportunity1', AccountID = acc1.id, Probability = 20.00, CloseDate = Date.newInstance(System.now().year(),10,10), StageName = 'Prospect');
insert opp1;
ApexPages.StandardController sc = new ApexPages.standardController(opp1);
ControllerExt cls = new ControllerExt(sc);
cls.save();
All Answers
Try this
@isTest
public class ControllerExtTest {
static testMethod void myPage_Test() {
PageReference pageRef = Page.TypeA;
Test.setCurrentPage(pageRef);
Account acc1 = new Account (Name = 'Account 1', Type = 'Type A');
insert acc1;
Opportunity opp1 = new Opportunity(Name = 'Opportunity1', AccountID = acc1.id, Probability = 20.00, CloseDate = Date.newInstance(System.now().year(),10,10), StageName = 'Prospect');
insert opp1;
ApexPages.StandardController sc = new ApexPages.standardController(opp1);
ControllerExt cls = new ControllerExt(sc);
cls.save();
Great thank you!