Test class for PageReference method

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

asked Dec 22, 2015 at 6:22 703 4 4 gold badges 15 15 silver badges 42 42 bronze badges

Why are you overriding the existing save method in StandardController instead of just using it on this page for a new opportunity? It does all the work for you. Simply use in an action on your VF, no code necessary, and it redirects automatically.

Commented Dec 22, 2015 at 7:22

I don't understand the point of your code. All it does is save a new opportunity with hardcoded name, stage and today's date, and returns a page reference to it. I think that a standard controller could do that. Nevertheless, if you still want to test this, you need to assert 2 things: that a new opportunity is created, and that the PageReference you get points to its id.

Commented Dec 22, 2015 at 8:16

I was just playing with the custom controller so wanted to test it. And I'm hardcoding the values in the test class and not in the controller class.

Commented Dec 22, 2015 at 8:19

3 Answers 3

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); >> 
answered Dec 22, 2015 at 6:24 Tushar Sharma Tushar Sharma 29.6k 7 7 gold badges 38 38 silver badges 60 60 bronze badges

Yeah well those 3 are the only required fields in Opportunity. My error is 'REQUIRED_FIELD_MISSING, Required fields are missing: [Name, StageName, CloseDate]: [Name, StageName, CloseDate]'.

Commented Dec 22, 2015 at 6:32 @User2529 Please check my updated answer. Commented Dec 22, 2015 at 6:58

Slight correction. It should be apexPages.Currentpage().getParameters().put('Id',oppC.opp.id); in your code :)

Commented Dec 22, 2015 at 7:51

I don't understand why this tests puts the opportunity's ID in the parameters of the page, or why it edit's oppC.opp to match the values that are already there. IMHO the test should call save() and assert that a new opportunity has been created and that the ID return matches the opp Id.

Commented Dec 22, 2015 at 8:18

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

answered Dec 22, 2015 at 6:30 1,847 4 4 gold badges 26 26 silver badges 41 41 bronze badges

Yeah well those 3 are the only required fields in Opportunity. My error is 'REQUIRED_FIELD_MISSING, Required fields are missing: [Name, StageName, CloseDate]: [Name, StageName, CloseDate]'.

Commented Dec 22, 2015 at 6:32 Why are you trying to insert opp in test class when your actual controller is already doing that Commented Dec 22, 2015 at 6:43 Removed 'insert opp;' if that's what you meant but it's still not working. Commented Dec 22, 2015 at 6:49

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'); ListinsertedOpps = [Select Id from Opportunity]; System.AssertEquals (1, insertedOpps.size(), 'Expected to have 1 opportunity in the system'); > > 

I have also done some other improvements:

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