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
girbot56girbot56 

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);
    }
}

 

Best Answer chosen by Admin (Salesforce Developers) 
New_DeveloperNew_Developer

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

New_DeveloperNew_Developer

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();

This was selected as the best answer
girbot56girbot56

Great thank you!