0

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;
}

1 Answer 1

1

While you set the products field to the 100 products here in your test;

MC.products = data;

as part of the removeContact method that field is changed to:

products = database.query(GETQuery);

So it looks like in your test scenario that query returns less than 6 rows.

PS

Based on the addition to the question, the problem is that the rows returned by createData are never inserted so when the query is done no rows are returned.

4
  • GETQuery is a final string: GETQuery = 'SELECT Id, Name, Price__c, Quantity__c, Type__c, Date_Created__c, Date_Added__c, Available__c FROM Merchandise__c'; so it just returns list of all merchandise, i suppose
    – squaremilk
    Commented May 28, 2015 at 17:49
  • A method called MerchandiseControllerTest.createData() sounds like it might just create the data and not insert it which would result in the query returning nothing.
    – Keith C
    Commented May 28, 2015 at 17:50
  • I added code for createData(). Plus assert which comes before is working like a charm: System.assertEquals(MC.products.size(),100);
    – squaremilk
    Commented May 28, 2015 at 17:55
  • The 100 rows are present in memory but not in the database. To be in the database they must be inserted.
    – Keith C
    Commented May 28, 2015 at 17:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .