Activity Forums Salesforce® Discussions How to write test class for code coverage of pagereference method in Salesforce?

  • Abhinav

    Member
    May 27, 2016 at 1:26 pm

    Hi Ajay,

    Min__c min1 = new Min__c();
    min1.name='test1';
    min1.SMS_Description__c ='test3';
    insert min1;

    PageReference pageRef = Page.ExhibitAadminTier; // Adding VF page Name here
    pageRef.getParameters().put('id', String.valueOf(min1.id));//for page reference
    Test.setCurrentPage(pageRef);
    ApexPages.currentPage().getParameters().put('RecordType','ExhibitAadminTier');
    ApexPages.StandardController sc = new ApexPages.standardController(min1);
    ExhibitAadminTier controller = new ExhibitAadminTier(sc);
    controller.save1();//Page Reference method name

  • Parul

    Member
    September 29, 2018 at 1:18 am

    Apex class:

    public with sharing class opportunityController{
    public Opportunity opp {get;set;}
    public opportunityController(){
    opp = new Opportunity ();
    }

    public pageReference save(){
    insert opp;
    pagereference page =new ApexPages.StandardController(opp).view();//pageReference ('/apex/OpportunityPage');
    page.setRedirect(true);
    return page;
    }
    }

    Test Class:

    @isTest

    public class testOpportunityController{
    public static testMethod void testOpp () {
    opportunityController oppC = new opportunityController ();
    pageReference pager = page.OpportunityPage;
    Test.setCurrentPage(pager);
    oppC.opp.Name = 'abc';
    oppC.opp.Stagename = 'Prospecting';
    oppC.opp.Closedate = system.today();
    oppC.save();
    system.assert(oppC.opp.Id != null);

    apexPages.Currentpage().getParameters().put('Id',opp.id);
    }
    }

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos