1

I have a fairly simple custom controller that redirects to various pages based upon selection of record type when creating a new Case from an Account or the Case tab. I am trying to develop a test class to cover the controller, but I'm not quite sure how to cover the method. My class below only covers 36% and seems to only cover the 2nd condition of the redirect (i.e., where AccountId == null). Can anyone help get my test to cover the other 2 conditions?

VF Controller:

public with sharing class VF_Controller_CaseAcctSvcsNew{

public Case c1;

    public VF_Controller_CaseAcctSvcsNew(ApexPages.StandardController controller){
        this.c1 = (Case)controller.getRecord();
    }


    public PageReference CaseRedirect() {
        if(ApexPages.currentPage().getParameters().get('RecordType') == '012L0000000DPwQ'){
            DateTime d = Date.Today();
            c1.OwnerId='00GL0000001SUhm';
            c1.Due_Date_Original__c = System.now() + 1;
            c1.Due_Date_AcctSvcs__c = System.now() + 1;
            c1.Status = 'New';
            PageReference pageRef = new PageReference('/apex/VF_AcctSvcsNew');
            return pageRef;
        }
        else if(ApexPages.currentPage().getParameters().get('AccountId') == null){
            PageReference pageRef2 = new PageReference('https://cs8.salesforce.com/500/e?RecordType='+c1.RecordTypeId+'&ent=Case&nooverride=1');
            return pageRef2;
        }
        else{
            PageReference pageRef3 = new PageReference('https://cs8.salesforce.com/500/e?def_account_id='+c1.AccountId+'&RecordType='+c1.RecordTypeId+'&ent=Case&nooverride=1');
            return pageRef3;
        }
    }
}

Test Class:

@IsTest (SeeAllData=true)
private class TestControllerAcctSvcsCaseNew{

    static testMethod void testASCcontroller1(){

        Account acct1 = TestCreateRecords.createAcct(0);
        insert acct1;

        Contact cont1 = TestCreateRecords.createContNew(acct1.Id);
        insert cont1;

        Case case1 = new Case();
            case1.RecordTypeId = '012L0000000DPwQ';
            case1.AccountId = acct1.Id;
            case1.OwnerId = '00GL0000001SUhm';
            case1.Due_Date_Original__c = System.now() + 1;
            case1.Due_Date_AcctSvcs__c = System.now() + 1;
            case1.Status = 'New';

        ApexPages.StandardController cs1 = new ApexPages.standardController(case1);
        VF_Controller_CaseAcctSvcsNew cs1a = new VF_Controller_CaseAcctSvcsNew(cs1);

        cs1a.c1 = case1;
        cs1a.CaseRedirect();
    }
}

2 Answers 2

3

In your method CaseRedirect you have an IF condition based on page parameters.

In your test code, you do not specify any page parameters so it will always go to the "Else" part of the logic.

You need something like this in your test class

Test.setCurrentPageReference(new PageReference('Page.myPage'));
System.currentPageReference().getParameters().put('RecordType', thevalueyouwant);
3
  • Thanks so much! I added those lines before calling the standard controller in my test and they covered the 1st 2 conditions of the IF statement in my controller. The 3rd condition is still not covered. Any idea why or how to cover that one?
    – J. Neilan
    Jun 2, 2016 at 17:03
  • for third method, call the CaseRedirect() method again without setting any parameters, it should cover all 3 in this case.
    – Anurag
    Jun 2, 2016 at 17:24
  • @Anurag A -- Thanks. I did remove the parameters from the 3rd method, but it still did not cover my 3rd condition.
    – J. Neilan
    Jun 2, 2016 at 17:31
1

The problem is that, in you class, you're only checking the parameters.

You have two options:

1) Change you class so instead of ApexPages.currentPage().getParameters().get('AccountId') == null you use c1.AccountId

2) Set the Parameters on the test class:

PageReference pg = Page.UserShare_FromAccount;
pg.getParameters().put('AccountId', case.AccountId);
pg.getParameters().put('RecordType', case.RecordTypeId);
Test.setCurrentPage(contactList);

ApexPages.StandardController cs1 = new ApexPages.standardController(case1);
VF_Controller_CaseAcctSvcsNew cs1a = new VF_Controller_CaseAcctSvcsNew(cs1);

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .