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
Gannon MiddletonGannon 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!

Prateek Prasoon 25Prateek Prasoon 25
@isTest
private class RedirectControllerTest {
    @isTest
    static void testRedirectController1() {
        Client__c client = new Client__c(extid_client__c='123');
        insert client;
        RedirectController controller = new RedirectController();
        controller.itemName = 'Client';
        controller.externalID = '123';
        controller.ac = null;
        controller.lic = null;
        controller.opp = null;
        PageReference pageRef = controller.redirect();
    }
        @isTest
    static void testRedirectController1() {
        License__c license = new License__c(extid_license__c='456');
        insert license;
        // Test redirect for License
        RedirectController controller = new RedirectController();
        controller.itemName = 'License';
        controller.externalID = '456';
        controller.ac = null;
        controller.lic = null;
        controller.opp = null;
        pageRef = controller.redirect();
    }
        @isTest
    static void testRedirectController3() {
        Opportunity opportunity = new Opportunity(extid_opportunity__c='789');
        insert opportunity;
        RedirectController controller = new RedirectController();
        controller.itemName = 'Opportunity';
        controller.externalID = '789';
        controller.ac = null;
        controller.lic = null;
        controller.opp = null;
        pageRef = controller.redirect();
    }

If you find this answer helpful, Please mark it as the best answer.
Gannon MiddletonGannon Middleton

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();

SwethaSwetha (Salesforce Developers) 
HI @Gannon,
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