You need to sign in to do that
Don't have an account?
Gannon Middleton
APEX Redirect Test Class
Hi all,
I am a novice, looking to write a test class for a redirect.
public class RedirectController { public String itemName { get; set; } public String externalID { get; set; } public String SFID { get; set; } public Client ac { get; set; } public License lic { get; set; } public Opportunity opp { get; set; } public RedirectController() { itemName = ApexPages.currentPage().getParameters().get('itemName'); externalID = ApexPages.currentPage().getParameters().get('externalID'); if (itemName == 'Client') { ac = [SELECT Id FROM Client__c WHERE extid_client__c = :externalID limit 1]; SFID = ac.Id; } if (itemName == 'License') { lic = [SELECT Id FROM License__c WHERE extid_license__c = :externalID limit 1]; SFID = lic.Id; } if (itemName == 'Opportunity') { opp = [SELECT Id FROM Opportunity WHERE extid_opportunity__c = :externalID limit 1]; SFID = opp.Id; } } public PageReference redirect() { PageReference pageRef = new PageReference('/' + SFID); pageRef.setRedirect(true); return pageRef; } }
The VF page is then:
<apex:page controller="RedirectController" action="{!redirect}"> </apex:page>
The above all works, but I can't deploy to Production without a test class and I am not even sure what I am testing, I've written them before with dummy data, but this one has be a bit confused.
Any direction would be greatly appreciated!
If you find this answer helpful, Please mark it as the best answer.
Hi Prateek, this is still coming up as Code Coverage 0% for me?
I needed to rename the second test to be testRedirectController2 and the last lines to be PageReference pageRef = controller.redirect();
Can you share an image of which lines of code are uncovered?
Recommend reviewing:
https://salesforce.stackexchange.com/questions/244794/how-do-i-increase-my-code-coverage-or-why-cant-i-cover-these-lines
Code Coverage Best Practices
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_code_coverage_best_pract.htm
Checking Code Coverage
https://help.salesforce.com/articleView?id=code_dev_console_tests_coverage.htm&type=5
Deployment-related code coverage issues
https://help.salesforce.com/articleView?id=000335222&type=1&mode=1
https://salesforce.stackexchange.com/questions/148894/help-required-to-get-full-code-covered-in-test-class
Thanks