I have a few page redirects that do not want to work in my test class. The error I am running into is the partial URL reference.
ERROR: System.ListException: List index out of bounds: 0 Class.ACHBounceControllerTest.startAllOpenCases: line 9, column 1. pageReference pg = new PageReference('/' + openCases[0].Id);
I insert a value into the list so openCases[0].Id
can't be empty. Am I syntactically writing it incorrectly?
@isTest
public with sharing class ACHBounceControllerTest {
public static testmethod void nextCaseTest(){
test.startTest();
RecordType rt = new RecordType(Name = 'ACH Bounce');
Case ca = new Case(Status='On Hold',Last_Stage_Change_Date__c = Datetime.now(), RecordType = rt, lastInArray__c = FALSE);
insert ca;
List<Case> openCases = [SELECT Id, Status, CaseNumber,Last_Stage_Change_Date__c FROM Case WHERE (RecordType.Name = 'ACH Bounce' AND (Status = 'Sent to Accounting' OR Status ='On Hold') AND lastInArray__c = false) ORDER BY Last_Stage_Change_Date__c ASC];
pageReference pg = new PageReference('/' + openCases[0].Id);
Test.setCurrentPage(pg);
ApexPages.StandardController sc = new ApexPages.standardController(ca);
ACHBounceController controller = new ACHBounceController(sc);
controller.nextCase();
test.stopTest();
}
}
APEX:
public PageReference nextCase() {
if((openCases[0].Status == 'Sent to Accounting') || (openCases[0].Status == 'On Hold')) {
system.debug('page: ' + '/' + openCases[0].Id);
PageReference ReturnPage = new PageReference('/' + openCases[0].Id);
ReturnPage.setRedirect(true);
return ReturnPage;
}
return null;
}
UPDATES:
Attempted to update the RecordType correctly:
public static testmethod void nextCase(){
test.startTest();
//Did not work--> Id recordTypId = Schema.getGlobalDescribe().get('Case').getDescribe().getRecordTypeInfosByName().get('ACH Bounce').getRecordTypeId());
RecordTypeInfo recordTypId = Schema.getGlobalDescribe().get('Case').getDescribe().getRecordTypeInfosByName().get('ACH Bounce');
system.debug('rec type id: ' + recordTypId);
Case ca = new Case(Status='On Hold',Last_Stage_Change_Date__c = Datetime.now(), RecordType = recordTypId, lastInArray__c = FALSE);
insert ca;
system.debug('test ca: ' + ca);
List<Case> testopenCases = [SELECT Id, Status, CaseNumber,Last_Stage_Change_Date__c FROM Case WHERE (RecordType.Name = 'ACH Bounce' AND (Status = 'Sent to Accounting' OR Status ='On Hold') AND lastInArray__c = false) ORDER BY Last_Stage_Change_Date__c ASC];
system.debug('test testopenCases: ' + testopenCases);
//pageReference pg = new PageReference('/' + openCases.Id);
pageReference pg = new PageReference('/' + ca.Id);
Test.setCurrentPage(pg);
ApexPages.StandardController sc = new ApexPages.standardController(ca);
ACHBounceController controller = new ACHBounceController(sc);
controller.openCases = testopenCases;
controller.nextCase();
test.stopTest();
}
No success - I am able to see that the Case is inserted and there is a record type id but the list is null- see picture.
UPDATE 2:
What I have found is that the SOQL query is not able to see the inserted records because of this part of the query: (Status = 'Sent to Accounting' OR Status ='On Hold')
. Status is a picklist field and I am unable to insert it's value.
public static testmethod void nextCaseTest(){
test.startTest();
Id recordTypId = Schema.getGlobalDescribe().get('Case').getDescribe().getRecordTypeInfosByName().get('ACH Bounce').getRecordTypeId();
Case ca = new Case(Status='Sent to Accounting', RecordTypeid = recordTypId, lastInArray__c = True);
insert ca;
Case ca2 = new Case(Status='On Hold', RecordTypeid = recordTypId, lastInArray__c = True);
insert ca2;
Case ca3 = new Case(Status='Sent to Accounting', RecordTypeid = recordTypId, lastInArray__c = True);
insert ca3;
system.debug('ca: ' + ca);
List<Case> openCases = [SELECT Id, Status, CaseNumber,Last_Stage_Change_Date__c FROM Case WHERE RecordType.Name = 'ACH Bounce' AND (Status = 'Sent to Accounting' OR Status ='On Hold') AND lastInArray__c = TRUE ORDER BY Last_Stage_Change_Date__c ASC];
system.debug('2openCases: ' + openCases);
pageReference pg2 = new PageReference('/' + openCases[0].Id);
system.debug('2openCases[0].Id: ' + openCases[0].Id);
Test.setCurrentPage(pg2);
ApexPages.StandardController sc = new ApexPages.standardController(ca);
ACHBounceController controller = new ACHBounceController(sc);
controller.openCases = openCases;
controller.nextCase();
test.stopTest();
}