I cannot understand whats going wrong in this unit test. Last assert is failing: array is out of bonds. Thats a fragment from test class:
static testMethod void testRemoveContact() {
MerchandiseController MC = new MerchandiseController();
List<Merchandise__c> data = MerchandiseControllerTest.createData();
MC.products = data;
PageReference ref = new PageReference('/apex/MerchandiseList');
Test.setCurrentPage(ref);
ApexPages.currentPage().getParameters().put('index','5');
Id id = MC.products[5].Id;
string id1 = ref.getParameters().get('index');
System.assert(id1=='5');
System.assertEquals(MC.products.size(),100);
MC.removeContact();
System.assertNotEquals(MC.products[5].Id, id);
}
and the removeContact method from MerchandiseController works as follows:
public void removeContact() {
Integer rowNum = Integer.valueof(system.currentpagereference().getparameters().get('index'));
if(products[rowNum].Id != null) {
database.delete(products[rowNum].Id);
}
products = database.query(GETQuery);
}
createData method
private static List<Merchandise__c> createData() {
List<Merchandise__c> dataInput = new List<Merchandise__c>();
Merchandise__c prod = new Merchandise__c();
for (Integer i = 0; i < 100; i++) {
prod = new Merchandise__c(Name = 'product'+i, Price__c=300*Math.random(), Quantity__c=10*Math.random(), Type__c='type'+i,
Date_Added__c=Date.parse('5/26/2015'), Date_Created__c=Date.parse('5/26/2015'),
Available__c=true);
dataInput.add(prod);
}
return dataInput;
}