0

I've recently been writing a few basic controllers for some visual force pages, and of course now need to test them. Problem is I don't really know how. Normally you know you'd instantiate the class, pass data to the methods, and assert against the results right? With this controller I don't know what to test against.

It would be great if someone could help me write a test class for this.

String selectedRTypeId;
Id compMagId;
String accountId;
Id compNewsId;
String contactId;
Id changePaymentId;
Id changeCreditCard;
Id bypassCreditCard;
Id adjustmentsRType;
Id changeAddressId;
Id startSubId;
Id stopSubId;
Id changeParticularsId;
Id resetPassId;
Id retentionId;
Id tempStopId;
Id refundId;
Id digitalId;
Id enquiryId;
Id changeOfSubId;
Id account_DirectCorporate;
Id account_DirectIndividual;
Id account_DirectSphInterDivision;
Id account_Vendor;
Id account_VendorSubscriber;
Id renewalSubId;

Account acct;
List<Order__c> orderRecs;
Order__c orderRec = new Order__c();


public CaseNewOverrideController (ApexPages.StandardController controller) {
    accountId = ApexPages.currentPage().getParameters().get('def_account_id');
    contactId = ApexPages.currentPage().getParameters().get('def_contact_id');
    selectedRTypeId = ApexPages.currentPage().getParameters().get('RecordType');
    compMagId = ConstantsSLB.getKeyId('Case_Complaint Magazine');
    compNewsId = ConstantsSLB.getKeyId('Complaint_Newspaper');
    changePaymentId = ConstantsSLB.getKeyId('Case_Change of Payment Type');
    changeCreditCard = ConstantsSLB.getKeyId('Case_Change of Credit Card');
    bypassCreditCard = ConstantsSLB.getKeyId('Case_Bypass Credit Card');
    adjustmentsRType = ConstantsSLB.getKeyId('Case_Adjustments');
    changeAddressId = ConstantsSLB.getKeyId('Case_Change in Address');
    startSubId = ConstantsSLB.getKeyId('Case_Start Sub');
    stopSubId = ConstantsSLB.getKeyId('Case_Stop');
    changeParticularsId = ConstantsSLB.getKeyId('Case_Particulars');
    resetPassId = ConstantsSLB.getKeyId('Case_Reset Password');
    retentionId = ConstantsSLB.getKeyId('Case_Retention');
    tempStopId = ConstantsSLB.getKeyId('Case_Temp Stop');
    refundId = ConstantsSLB.getKeyId('Case_Refund');
    digitalId = ConstantsSLB.getKeyId('Case_Digital'); 
    enquiryId  = ConstantsSLB.getKeyId('Case_Enquiry');
    changeOfSubId = ConstantsSLB.getKeyId('Case_Change of Subscription');
    account_DirectCorporate = ConstantsSLB.getKeyId('Account_Direct Corporate');
    account_DirectIndividual = ConstantsSLB.getKeyId('Account_Direct Individual');
    account_DirectSphInterDivision = ConstantsSLB.getKeyId('Account_Direct SPH Inter-Division'); 
    account_Vendor = ConstantsSLB.getKeyId('Account_Vendor');
    account_VendorSubscriber = ConstantsSLB.getKeyId('Account_Vendor Subscriber');  
    account_VendorSubscriber = ConstantsSLB.getKeyId('Account_Vendor Subscriber');
    renewalSubId = ConstantsSLB.getKeyId('Case Renewal of Subscription');       

    if(accountId != null && accountId != '') {
        try {
            acct = [Select RecordTypeId FROM Account WHERE Id = :accountId];
            orderRecs = [SELECT Id FROM Order__c WHERE Account__c = :acct.Id AND (Status__c = 'Draft' OR Status__c = 'New') LIMIT 1];
        }
        catch(Exception e) {
            acct = new Account();
            orderRecs = new List<Order__c>();
        }
    }

}

public PageReference redirect() {
    String pRef;
    if(selectedRTypeId == compMagId) {          
        pRef = '/apex/ComplaintMagazineEditPage?RecordType=' + selectedRTypeId +
                    (accountId != null && accountId != 'null' && accountId != '' ? '&accountId=' + accountId : '') +
                    (contactId != null && contactId != 'null' && contactId != '' ? '&contactId=' + contactId : '');
    }        
    else if (selectedRTypeId == compNewsId ) {
        pRef = '/apex/ComplaintNewspaperPage?RecordType=' + selectedRTypeId +
                    (accountId != null && accountId != 'null' && accountId != '' ? '&accountId=' + accountId : '') +
                    (contactId != null && contactId != 'null' && contactId != '' ? '&contactId=' + contactId : '');     
    }   

    else if (selectedRTypeId == changeCreditCard ) {
        pRef = '/apex/CustomHpmPageCase?RecordType=' + selectedRTypeId +
                    (accountId != null && accountId != 'null' && accountId != '' ? '&accountId=' + accountId : '') +
                    (contactId != null && contactId != 'null' && contactId != '' ? '&contactId=' + contactId : '');     
    } 

    else if (selectedRTypeId == changePaymentId ) {
        pRef = '/apex/ChangeOfPaymentType?RecordType=' + selectedRTypeId +
                    (accountId != null && accountId != 'null' && accountId != '' ? '&accountId=' + accountId : '') +
                    (contactId != null && contactId != 'null' && contactId != '' ? '&contactId=' + contactId : '');     
    } 

    else if (selectedRTypeId  == renewalSubId ) {
        pRef = '/apex/RenewalOfSubscriptionPage?RecordType=' + selectedRTypeId  + 
                    (accountId != null && accountId != 'null' && accountId != '' ? '&accountId=' + accountId : '') +
                    (contactId != null && contactId != 'null' && contactId != '' ? '&contactId=' + contactId : '');
    }

    else if (selectedRTypeId == adjustmentsRType ) {
        pRef = '/apex/AdjustmentsCaseEditPage?RecordType=' + selectedRTypeId +
                    (accountId != null && accountId != 'null' && accountId != '' ? '&accountId=' + accountId : '') +
                    (contactId != null && contactId != 'null' && contactId != '' ? '&contactId=' + contactId : '');     
    }
    else if (selectedRTypeId == changeAddressId ) {
        pRef = '/apex/ChangeInAddressCaseNewPage?RecordType=' + selectedRTypeId +
                    (accountId != null && accountId != 'null' && accountId != '' ? '&accountId=' + accountId : '') +
                    (contactId != null && contactId != 'null' && contactId != '' ? '&contactId=' + contactId : '');     
    }

    else if(selectedRTypeId == startSubId) {
        if (accountId != null) {
            System.debug('>>>>>>>> OrderRecs: ' + orderRecs);
            System.debug('>>>>>>>> account: ' + acct);
            System.debug('>>>>>>>> accountId: ' + accountId);
            System.debug('>>>>>>>> contactId: ' + contactId);
            if(acct.RecordTypeId == account_VendorSubscriber) {
                if(orderRecs.size() == 1) {
                    orderRec = orderRecs[0];
                }
                if(orderRec.Id != null) {
                    System.debug('>>>>>> Path 1');
                    pRef = '/apex/ViewVendorOrderEntry?RecordType=' + selectedRTypeId +
                            (accountId != null && accountId != 'null' && accountId != '' ? '&accountId=' + accountId : '') +
                            (contactId != null && contactId != 'null' && contactId != '' ? '&contactId=' + contactId : '');
                }
                else {
                    System.debug('>>>>>> Path 2');
                    pRef = '/apex/CreateVendorOrderEntry?RecordType=' + selectedRTypeId +
                            (accountId != null && accountId != 'null' && accountId != '' ? '&accountId=' + accountId : '') +
                            (contactId != null && contactId != 'null' && contactId != '' ? '&contactId=' + contactId : '');
                }
            }
            else {
                if(orderRec.Id != null) {
                    System.debug('>>>>>> Path 3');
                    pRef = '/apex/SM_StartSubscriptionDetail?RecordType=' + selectedRTypeId + '&id=' + orderRec.Id +
                        (accountId != null && accountId != 'null' && accountId != '' ? '&accountId=' + accountId : '') +
                        (contactId != null && contactId != 'null' && contactId != '' ? '&contactId=' + contactId : '');  
                }
                else {
                    System.debug('>>>>>> Path 4');
                    pRef = '/apex/SM_StartSubscriptionEntry?RecordType=' + selectedRTypeId +
                            (accountId != null && accountId != 'null' && accountId != '' ? '&accountId=' + accountId : '') +
                            (contactId != null && contactId != 'null' && contactId != '' ? '&contactId=' + contactId : '');
                } 
            }
        } else {
            pRef = '/500/e?nooverride=1&RecordType=' + selectedRTypeId + '&retURL=/500/o' + 
                         (accountId != null && accountId != 'null' && accountId != '' ? '&def_account_id=' + accountId : '') +
                        (contactId != null && contactId != 'null' && contactId != '' ? '&def_contact_id=' + contactId : '');
        }
        return new PageReference(pRef);
    }

    else if (selectedRTypeId == changeOfSubId) {
        pRef = '/apex/OrderFormChangeofSubPackage?RecordType=' + selectedRTypeId +
                    (accountId != null && accountId != 'null' && accountId != '' ? '&accountId=' + accountId : '') +
                    (contactId != null && contactId != 'null' && contactId != '' ? '&contactId=' + contactId : '');     
    }

    else if(selectedRTypeId == stopSubId) {

    if (accountId != null) {
        System.debug('>>>>>>>> OrderRecs: ' + orderRecs);
        System.debug('>>>>>>>> account: ' + acct);
        System.debug('>>>>>>>> accountId: ' + accountId);
        System.debug('>>>>>>>> contactId: ' + contactId);
        if(acct.RecordTypeId == account_VendorSubscriber) {
            if(orderRecs.size() == 1) {
                orderRec = orderRecs[0];
            }
            if(orderRec.Id != null) {
                System.debug('>>>>>> Path 1');
                pRef = '/apex/StopSubOrderEntry_Vendor_View?RecordType=' + selectedRTypeId +
                        (accountId != null && accountId != 'null' && accountId != '' ? '&accountId=' + accountId : '') +
                        (contactId != null && contactId != 'null' && contactId != '' ? '&contactId=' + contactId : '');
            }
            else {
                System.debug('>>>>>> Path 2');
                pRef = '/apex/StopSubOrderEntry_Vendor_Edit?RecordType=' + selectedRTypeId +
                        (accountId != null && accountId != 'null' && accountId != '' ? '&accountId=' + accountId : '') +
                        (contactId != null && contactId != 'null' && contactId != '' ? '&contactId=' + contactId : '');
            }
        }
        else {
                if(orderRec.Id != null) {
                    System.debug('>>>>>> Path 3');
                    pRef = '/apex/StopSub_Order_Entry_Direct_View?RecordType=' + selectedRTypeId + '&id=' + orderRec.Id +
                        (accountId != null && accountId != 'null' && accountId != '' ? '&accountId=' + accountId : '') +
                        (contactId != null && contactId != 'null' && contactId != '' ? '&contactId=' + contactId : '');  
                }

                else {
                    System.debug('>>>>>> Path 4');
                    pRef = '/apex/StopSub_Order_Entry_Direct_Edit?RecordType=' + selectedRTypeId +
                            (accountId != null && accountId != 'null' && accountId != '' ? '&accountId=' + accountId : '') +
                            (contactId != null && contactId != 'null' && contactId != '' ? '&contactId=' + contactId : '');
                }
            }
        }  else {
            pRef = '/500/e?nooverride=1&RecordType=' + selectedRTypeId + '&retURL=/500/o' + 
                         (accountId != null && accountId != 'null' && accountId != '' ? '&def_account_id=' + accountId : '') +
                        (contactId != null && contactId != 'null' && contactId != '' ? '&def_contact_id=' + contactId : '');
        }
        return new PageReference(pRef);
    }

    else if (selectedRTypeId == tempStopId) {
        if (acct.RecordTypeId == account_DirectIndividual || acct.RecordTypeId == account_DirectCorporate || acct.RecordTypeId == account_DirectSphInterDivision) {
        pRef = 'https://c.cs6.visual.force.com/apex/DirectUpdateTempStopSubs?RecordType=' + selectedRTypeId +
                    (accountId != null && accountId != 'null' && accountId != '' ? '&accountId=' + accountId : '') +
                    (contactId != null && contactId != 'null' && contactId != '' ? '&contactId=' + contactId : '');   
        }
        else if (acct.RecordTypeId == account_VendorSubscriber) 
        {pRef = '/apex/VendorTempStopSubs?RecordType=' + selectedRTypeId +
                    (accountId != null && accountId != 'null' && accountId != '' ? '&accountId=' + accountId : '') +
                    (contactId != null && contactId != 'null' && contactId != '' ? '&contactId=' + contactId : '');     
    }
    }

    else {
        pRef = '/500/e?nooverride=1&RecordType=' + selectedRTypeId + '&retURL=/500/o' + 
                     (accountId != null && accountId != 'null' && accountId != '' ? '&def_account_id=' + accountId : '') +
                    (contactId != null && contactId != 'null' && contactId != '' ? '&def_contact_id=' + contactId : '');
    }
    return new PageReference(pRef);
}
1

Very long story short, you answered your own question.

Normally you know you'd instantiate the class, pass data to the methods, and assert against the results right?

You are correct. I think you may be confused on how to assert the pageReference that is returned is correct. So for this class you really just need to keep resetting the recordTypeId, and calling your redirect() method. then you just need to assert the output of that method, which is a PageReference, that has a URL, with a URL that you expect to be returned.

So for example, this would test a portion of your redirect() method. (Assuming you set up some of the prelim data and instantiate the controller.)

controller.RecordTypeId = RECORD_ID_YOU_WANT_TO_TEST;
String returnPageURL = controller.redirect().getUrl();
system.assertEquals(returnPageURL, YOUR_EXPECTED_URL);

You would need to do something similar for all of the IF() branches in your code. Set the RecordTypeId, call your method, and assert the returnURL is the desired outcome.

1

There's information about that here

First you create your Visualforce page. Next, you build any necessary data. Finally, you attach the data to the controller and call the controller methods you need to test.

For a custom controller (not extension) that would look something like this:

PageReference pageRef = Page.ManageFieldTrips;
Test.setCurrentPageReference(pageRef);
...
//build any needed data
...
// Add parameters to page url
ApexPages.currentPage().getParameters().put('id',objTestSite1.ID);
...
// create an instance of the controller
FieldTripController myPageCon = new FieldTripController();
// set data on the controller and call controller methods to test it    
myPageCon.SelectFirst();
myPageCon.processSelected(); 

For simplicity, I've left out the asserts.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.