+ Start a Discussion
Prasanth Reddy MPrasanth Reddy M 

Test class for VF Controller

Hi,

For the below controller the test coverage is not exceeding 50%.
highlighted 2 lines are showing as not covered in test class.
Can someone please correct me on this?

Public with sharing class DispatcherAccountNewController {

public DispatcherAccountNewController(ApexPages.StandardController controller) {
        this.controller = controller;
    }

    public PageReference getRedir() {

        PageReference newPage;

        if (ApexPages.currentPage().getParameters().get('RecordType') == '0123B000000Hs88') {
            newPage = Page.newaccount;
            return newPage.setRedirect(true);
        }
            else if (ApexPages.currentPage().getParameters().get('RecordType') == '01232000000DxHz') {
            newPage = Page.abc_new_account;
            return newPage.setRedirect(true);

        }
            else if (ApexPages.currentPage().getParameters().get('RecordType') == '0120d0000009grC') {
            newPage = Page.health_integrated_new_account;
            return newPage.setRedirect(true);
        }     
             else if (ApexPages.currentPage().getParameters().get('RecordType') == '01232000000DxI0') {
            newPage = Page.olsi_new_account;
            return newPage.setRedirect(true);
        }   
            else {
      return null;
                    
        }

    }

    private final ApexPages.StandardController controller;
}

testclass - 
@isTest
private class DispatcherAccountNewController_Test{
  @testSetup
  static void setupTestData(){
    test.startTest();
    Account account_Obj = new Account(Name = 'Name897', RecordTypeId = '0123B000000Hs88');
    Insert account_Obj; 
    test.stopTest();
  }
  static testMethod void test_getRedir_UseCase1(){
    List<Account> account_Obj  =  [SELECT Name,RecordTypeId from Account];
    System.assertEquals(true,account_Obj.size()>0);
    PageReference newpage = Page.newaccount;
    newpage.getParameters().put('RecordTypeid','0123B000000Hs88');
    Test.setCurrentPage(newpage);
    DispatcherAccountNewController obj01 = new DispatcherAccountNewController(new ApexPages.StandardController(account_Obj[0]));
    obj01.getRedir();
  }
}
 
Best Answer chosen by Prasanth Reddy M
Ramesh DepaiahRamesh Depaiah
Your parameter name in testclass is different, please change that to match the controller parameter
it must be "RecordType" but you passed "RecordTypeid"

Try below test class
@isTest
private class DispatcherAccountNewController_Test{
    @testSetup
    static void setupTestData(){
        test.startTest();
        Account account_Obj = new Account(Name = 'Name897', RecordTypeId = '0123B000000Hs88');
        Insert account_Obj; 
        test.stopTest();
    }
    static testMethod void test_getRedir_UseCase1(){
        List<Account> account_Obj  =  [SELECT Name,RecordTypeId from Account];
        System.assertEquals(true,account_Obj.size()>0);
        PageReference newpage = Page.TreeGrid;
        newpage.getParameters().put('RecordType','0123B000000Hs88');
        Test.setCurrentPage(newpage);
        DispatcherAccountNewController obj01 = new DispatcherAccountNewController(new     ApexPages.StandardController(account_Obj[0]));
        obj01.getRedir();
        newpage.getParameters().put('RecordType','0120d0000009grC');
        Test.setCurrentPage(newpage);
        obj01 = new DispatcherAccountNewController(new ApexPages.StandardController(account_Obj[0]));
        obj01.getRedir();
        newpage.getParameters().put('RecordType','01232000000DxHz');
        Test.setCurrentPage(newpage);
        obj01 = new DispatcherAccountNewController(new ApexPages.StandardController(account_Obj[0]));
        obj01.getRedir();
        newpage.getParameters().put('RecordType','01232000000DxI0');
        Test.setCurrentPage(newpage);
        obj01 = new DispatcherAccountNewController(new ApexPages.StandardController(account_Obj[0]));
        obj01.getRedir();
        newpage.getParameters().put('RecordType','2345234523');
        Test.setCurrentPage(newpage);
        obj01 = new DispatcherAccountNewController(new ApexPages.StandardController(account_Obj[0]));
        obj01.getRedir();
    }
}

I hope you find the above solution helpful. If it does mark as best answer to help others too.
Thanks,
Ramesh D