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
MSVRadMSVRad 

How to Test Controller extension

I have this controller extension:

 

public class OpportunityClosedLost { public Opportunity opp; public apexpages.standardController controller {get; set;} public opportunityClosedLost(ApexPages.StandardController stdController) { // constructor controller = stdController; this.opp= (Opportunity)stdController.getRecord(); Opportunity opps = (Opportunity)stdController.getRecord(); opps = [SELECT RecordTypeId, StageName FROM Opportunity WHERE id=:system.currentpageReference().getparameters().get('id')]; //VRC opportunity if(opps.RecordTypeId =='012700000009RIb'){ isVrc = true; } //vRad Alliance Opportunity if(opps.RecordTypeId =='012T00000000JJD'){ isAlliance = true; } //EC Opportinity if(opps.RecordTypeId =='012700000005KwS'){ isEc = true; } } public List<SelectOption> getStageOptions(){ List<SelectOption> options = new List<SelectOption>(); options.add(new SelectOption('null','-- Select One --')); Schema.DescribeFieldResult oppStage = Schema.sObjectType.Opportunity.fields.StageName; for (Schema.PickListEntry oppPickVal : oppStage.getPicklistValues()){ // create a selectoption for each pickval options.add(new SelectOption(oppPickVal.getValue(),oppPickVal.getLabel())); } return options; } public Boolean isVrc{ get{ if(isVrc == null){ isVrc = false; } return isVrc; } set; } public Boolean isAlliance{ get{ if(isAlliance == null){ isAlliance = false; } return isAlliance; } set; } public Boolean isEc{ get{ if(isEc == null){ isEc = false; } return isEc; } set; } }

I have written a test class and it is only catching the true situations for the boolean properties colored in blue I am not sure how to test the false condition. I am also unsure how to test the SelectList portion of the code that is red.

 

Here is my test class so far:

 

 

public with sharing class OpportunityClosedLostTest { static testMethod void OpportunityClosedLostTest(){ Opportunity opp1 = new Opportunity(Name = 'Opportunity1', Probability = 20.00, CloseDate = Date.newInstance(System.now().year(),10,10), StageName = 'Prospect', RecordTypeId = '012700000009RIb'); insert opp1; Opportunity opp2 = new Opportunity(Name = 'Opportunity2', Probability = 70.00, CloseDate = Date.newInstance(System.now().year(),12,31), StageName = 'Quote Requested', RecordTypeId = '012700000005KwS'); insert opp2; Opportunity opp3 = new Opportunity(Name = 'Opportunity3', Probability = 50.00, CloseDate = Date.newInstance(System.now().year(),9,18), StageName = 'Competitive Situation', RecordTypeId = '012T00000000JJD'); insert opp3; //Test Opportunity Edit Page VRC Test.SetCurrentPageReference(New PageReference('Page.ClosedOpp')); ApexPages.Standardcontroller sc1 = New ApexPages.StandardController(opp1); System.CurrentPageReference().getParameters().put('id',opp1.Id); OpportunityClosedLost cOpp1 = new OpportunityClosedLost(sc1); System.debug(cOpp1.isVrc); update opp1; //Test Opportunity Detail Page VRC Test.setCurrentPageReference(New PageReference('Page.ClosedOppView')); ApexPages.StandardController sc2 = New ApexPages.StandardController(opp1); System.CurrentPageReference().getParameters().put('id',opp1.id); OpportunityClosedLost cOpp2 = new OpportunityClosedLost(sc2); System.debug(cOpp2.isVrc); update opp1; //Test Opportunity Edit Page EC Test.SetCurrentPageReference(New PageReference('Page.ClosedOpp')); ApexPages.StandardController sc3 = New ApexPages.StandardController(opp2); System.CurrentPageReference().getParameters().put('id', opp2.id); OpportunityClosedLost cOpp3 = new OpportunityClosedLost(sc3); System.debug(cOpp3.isEc); update opp2; //Test Opportunity Detail Page EC Test.SetCurrentPageReference(New PageReference('Page.ClosedOppView')); ApexPages.StandardController sc4 = New ApexPages.StandardController(opp2); System.CurrentPageReference().getParameters().put('id', opp2.id); OpportunityClosedLost cOpp4 = new OpportunityClosedLost(sc4); System.debug(cOpp4.isEc); update opp2; //Test Opportunity Edit Page vRad Alliance Test.SetCurrentPageReference(New PageReference('Page.ClosedOpp')); ApexPages.Standardcontroller sc5 = new ApexPages.Standardcontroller(opp3); System.currentPageReference().getParameters().put('id', opp3.id); OpportunityClosedLost cOpp5 = new OpportunityClosedLost(sc5); System.debug(cOpp5.isAlliance); update opp3; //Test Opportunity Detail Page vRad Alliance Test.setCurrentPageReference(New PageReference('Page.ClosedOppView')); ApexPages.StandardController sc6 = new ApexPages.StandardController(opp3); System.currentPageReference().getParameters().put('id', opp3.id); OpportunityClosedLost cOpp6 = new OpportunityClosedLost(sc6); System.debug(cOpp6.isAlliance); update opp3; } }

 

Best Answer chosen by Admin (Salesforce Developers) 
IanRIanR

Hi,

 

Coupe of things - firstly, you are using sytem.debug in your tests... try using system.assert() or system.assertEquals()

 

this will throw an exception if the assertion fails, and will show up as a test 'failure' in your debug log... this is a good thing!

 

So for the untested 'else' statements, when you look at opp1 being isVrc, ensure that it isn't Alliance or Ec:

 

system.AssertEquals( true, cOpp1.isVrc ); system.AssertEquals( false, cOpp1.isAlliance ); system.AssertEquals( false, cOpp1.isEc );

 

 

then repeat this with the appropriate logic for opp2 and opp3.

 

 

For the list getter, simply call the method and assert that a valid list is returned..

 

 

List<SelectOption> options = cOpp1.getStageOptions();

system.assert(options.size() > 0, 'option list was empty');

 

 

 

 

HTH, Ian

 

All Answers

IanRIanR

Hi,

 

Coupe of things - firstly, you are using sytem.debug in your tests... try using system.assert() or system.assertEquals()

 

this will throw an exception if the assertion fails, and will show up as a test 'failure' in your debug log... this is a good thing!

 

So for the untested 'else' statements, when you look at opp1 being isVrc, ensure that it isn't Alliance or Ec:

 

system.AssertEquals( true, cOpp1.isVrc ); system.AssertEquals( false, cOpp1.isAlliance ); system.AssertEquals( false, cOpp1.isEc );

 

 

then repeat this with the appropriate logic for opp2 and opp3.

 

 

For the list getter, simply call the method and assert that a valid list is returned..

 

 

List<SelectOption> options = cOpp1.getStageOptions();

system.assert(options.size() > 0, 'option list was empty');

 

 

 

 

HTH, Ian

 

This was selected as the best answer
china.leafchina.leaf

your extension controller class name is :  OpportunityClosedLost

 

add flow code to your test class:

 

(..... other part)

ApexPages.Standardcontroller sc1 = New ApexPages.StandardController(opp1);

OpportunityClosedLost   ext = new OpportunityClosedLost(sc1 );

(..... other part)

Message Edited by china.leaf on 09-01-2009 06:50 AM