You need to sign in to do that
Don't have an account?
Kris Hankins
Test Class help for extension
I have an apex class I found on line that will allow me to restrict a visualforce page override to specific Profiles, which is what I want to do. I have edited both VF pages to what I need them to be and implementing the apex class along with the VF pages works exactly how I need it to in the sandbox. I obviously have to create a test class for code coverage before moving this over to production. I ahve also found several tutorials on creating apex test classes, but none seem to cover what I need. Any assistance I can get on this would be great.
The apex class is:
The tabbedAccount VF page is below:
The standard Account page I will be using that contains the extension is:
The apex class is:
public class overrideCon { String recordId; public overrideCon(ApexPages.StandardController controller) {recordId = controller.getId();} public PageReference redirect() { Profile p = [select name from Profile where id = :UserInfo.getProfileId()]; if ('*Custom: Sales Pilot'.equals(p.name) ) { PageReference customPage = Page.tabbedAccount; customPage.setRedirect(true); customPage.getParameters().put('id', recordId); return customPage; } else { return null; //otherwise stay on the same page } } }
The tabbedAccount VF page is below:
<apex:page standardController="Account" action="{!If($Profile.Name != '*Custom: Sales Rep' || $Profile.Name !='*Custom: PSSR' || $Profile.Name !='*Custom: Executive' , null, urlFor($Action.Account.Tab, $ObjectType.Account, null, true))}" showHeader="true" tabStyle="account" > <style> .activeTab {background-color: red; color:white; background-image:none} .inactiveTab { background-color: lightgrey; color:black; background-image:none} </style> <apex:tabPanel switchType="client" selectedTab="tabdetails" id="AccountTabPanel" tabClass="activeTab" inactiveTabClass="inactiveTab"> <apex:tab label="Details" name="AccDetails" id="tabdetails"> <apex:detail relatedList="false" title="true"/> </apex:tab> <apex:tab label="Contacts" name="Contacts" id="tabContact"> <apex:relatedList subject="{!account}" list="contacts" /> </apex:tab> <apex:tab label="Opportunities" name="Opportunities" id="tabOpp"> <apex:relatedList subject="{!account}" list="opportunities" /> </apex:tab> <apex:tab label="Open Activities" name="OpenActivities" id="tabOpenAct"> <apex:relatedList subject="{!account}" list="OpenActivities" /> </apex:tab> <apex:tab label="Notes and Attachments" name="NotesAndAttachments" id="tabNoteAtt"> <apex:relatedList subject="{!account}" list="CombinedAttachments" /> </apex:tab> </apex:tabPanel> </apex:page>
The standard Account page I will be using that contains the extension is:
<apex:page standardController="account" extensions="overrideCon" action="{!redirect}"> <apex:detail /> </apex:page>
I wrote a very generic example above for you, but the gist is:
-
Create at least one test method for each public method in your controller / apex class. Ideally, you'll have a single test method for each condition you have in your method.
-
Create mock data so you dont have to rely on existing production data. In your case, you'll have to create an account and assign it to the StandardController you pass in to your extension's constructor.
-
Use Test.startTest / Test.stopTest around the method your testing to enforce it executes prior to your System assertion calls.
-
Use several system asserts to test that your criteria is what you would expect.
Hope that helps.All Answers
I wrote a very generic example above for you, but the gist is:
-
Create at least one test method for each public method in your controller / apex class. Ideally, you'll have a single test method for each condition you have in your method.
-
Create mock data so you dont have to rely on existing production data. In your case, you'll have to create an account and assign it to the StandardController you pass in to your extension's constructor.
-
Use Test.startTest / Test.stopTest around the method your testing to enforce it executes prior to your System assertion calls.
-
Use several system asserts to test that your criteria is what you would expect.
Hope that helps.Since System.assertEquals(Page.tabbedAccount.getUrl(),pageRef.getUrl()); for retrieving the UR, the page isn't being created? Or am I missing something that I should have added?
I modified the code and still got an assertion error for line 28. If I change that line to System.assertNotEquals(Page.tabbedAccount.getUrl(),pageRef.getUrl()); the test passes and gives the overrideCon class 90% coverage (10/11).