+ Start a Discussion
John Neilan 2John Neilan 2 

Test Class to Cover Custom Controller


I have 3 fairly simple custom controllers that redirect users s to various pages based upon selection of record type when creating a new Case from an Account or the Case tab, editing it, and viewing it.  The underlyiung VF pages for these controllers are simple redirects that override the standard buttons. I am trying to develop a test class to cover the controllers.  so far I have 2 of the controllers covered 89% and 75%, but I cannot figure out why the other lines are not being covered.  For Controller 1, my test class covers everything except Lines 22 -24.  For Controller 2 my test class covers everything except lines 11 and 12.  Can anyone help me get to 100% coverage and explain why they are not being covered?

Controller 1:
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.Due_Date_Original__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;
            PageReference pageRef3 = new PageReference('https://cs8.salesforce.com/500/e?def_account_id='+c1.AccountId+'&RecordType='+c1.RecordTypeId+'&ent=Case&nooverride=1');
            return pageRef3;
Controller 2:
public class VF_Controller_CaseAcctSvcsEdit {

    public Case c1;

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

    public PageReference AcctSvcsEditRedirect() {
        if (c1.RecordTypeId == '012L0000000DPwQ') {
            PageReference pageRef = new PageReference('/apex/VF_AcctSvcsEdit?id='+c1.Id);
            return pageRef;
        else {
            PageReference pageRef = new PageReference('https://cs8.salesforce.com/' + c1.id + '/e?retURL=%2F' + c1.id + '&nooverride=1');
            return pageRef;

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.AccountId = acct1.Id;
            case1.OwnerId = '00G57000005X4w4';
            case1.Due_Date_Original__c = System.now() + 1;
            case1.Status = 'New';
        insert case1;

        Test.setCurrentPageReference(new PageReference('Page.myPage1'));
        System.currentPageReference().getParameters().put('RecordType', '01257000000kg5H');

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

        cs1a.c1 = case1;

        VF_Controller_CaseAcctSvcsEdit cs11a = new VF_Controller_CaseAcctSvcsEdit(cs1);

        cs11a.c1 = case1;

//VF_Controller_CaseAcctSvcsNew - No associated Account
        Test.setCurrentPageReference(new PageReference('Page.myPage2'));
        System.currentPageReference().getParameters().put('RecordType', null);

        ApexPages.StandardController cs2 = new ApexPages.standardController(case1);
        VF_Controller_CaseAcctSvcsNew cs2a = new VF_Controller_CaseAcctSvcsNew(cs2);

        cs2a.c1 = case1;
        VF_Controller_CaseAcctSvcsEdit cs2b = new VF_Controller_CaseAcctSvcsEdit(cs2);

        cs2b.c1 = case1;

//VF_Controller_CaseAcctSvcsNew - All other conditions
        ApexPages.StandardController cs3 = new ApexPages.standardController(case1);
        VF_Controller_CaseAcctSvcsNew cs3a = new VF_Controller_CaseAcctSvcsNew(cs3);

        cs3a.c1 = case1;