You need to sign in to do that
Don't have an account?

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();
}
}
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();
}
}
it must be "RecordType" but you passed "RecordTypeid"
Try below test class
I hope you find the above solution helpful. If it does mark as best answer to help others too.
Thanks,
Ramesh D