Test class for PageReference method

Hey I’ve written a controller for my VF page with a PageReference method. The pagereference is working fine but I’m having some issues with the test class.

Controller 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 ();
        Opportunity opp = new Opportunity ();
        pageReference pager = page.OpportunityPage;
        Test.setCurrentPage(pager);
        opp.Name  = 'abc';
        opp.Stagename = 'Prospecting';
        opp.Closedate = system.today();
        insert opp;
        oppC.save();
        apexPages.Currentpage().getParameters().put('Id',opp.id);
   }
}

I’m getting an error stating ‘Required Fields Missing’. If someone could tell me where I’m going wrong, it’d be great!

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

you are missing some required field on opportunity insert. Please check and fill them it is not related to your method.

In class you are creating instance of Opportunity and in your test class you are creating another instance what you need to do here is

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

Method 2

Your this code is faulty.

opp.Name  = 'abc';
opp.Stagename = 'Prospecting';
opp.Closedate = system.today();
insert opp;

Check on Opportunity object for all required fields. then populate those fields as well like –

opp.requiredField = value

then insert the opp. It will work

Method 3

There is already an accepted answer, but I’m posting this because I think there is still some untested behaviour.

I assume that your production code is OK and I’m just focusing on the test (which I think should be written first!)

Your controller inserts a new opportunity and returns a page reference with its ID. Those two actions should be covered by tests:

@isTest
public class testOpportunityController {

    @isTest
    public static testMethod void OpportunityIsInsertedAndPageRefReturned () {
        opportunityController oppC = new opportunityController ();

        Test.startTest();
        pageReference response = oppC.save();
        Test.stopTest();

        System.assert (response != null, 'Expected to have a non null page response');
        String url = response.getUrl;
        System.assert (oppC.opp != null, 'Expected the opportunity in the controller to be non-null');
        String oppID = String.valueOf (oppC.opp.Id);

        system.assertEquals(url, oppId, 'Expected the page reference to contain the opportunity''s Id');

        List<Opportunity> insertedOpps = [Select Id from Opportunity];
        System.AssertEquals (1, insertedOpps.size(), 'Expected to have 1 opportunity in the system');
   }

}

I have also done some other improvements:

  • Added @isTest to the method (otherwise it will not run as a test!)
  • Added a more descriptive name to the test
  • Added string to show friendly error messages if assertions fail

There are more cases to test: what happens if you instantiate the controller, override the Opp field (which has a public setter!) and then call save()? What if you override it to null? Those cases are possible, so they must be covered if you want to specify a behaviour for them. Perhaps you want to make the setter private?

Hope you can find some of these comments useful


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x