[SalesForce] Can’t get Test.loadData method to run

I cannot get past various system exceptions trying to use the loadData method. It seems to be something with the csv file; changing it alters the cause of failure.

I am using data from production extracted today with DataLoader, Win7, and Force.com IDE. I copied the statement straight from the example and modified it to List<sObject> td = Test.loadData(Lead.sObjectType, 'testLeads200'); I have also verified both the test class and the class under test are using api 26.

When I leave encoding as ANSI, the failure is System.UnexpectedException: Salesforce System Error: 1067929596. Changing to UTF-8, with and without Unix formatted EOL, it seems to be readable, but… Leaving the double quote qualifiers in place gives a System.StringException: CSV Parse error: Found unescaped quote. Removing the qualifier, the failure becomes System.StringException: Unknown field: Division (or whatever the first header becomes after removing column). When uploading the file to Static Resources with Firefox, MIME type is text/csv; with Chrome, it is application/vnd.ms-excel.

I could find only a couple related issues- some comments on an answer here in our beloved StackExchange about the MIME type(which one not specified), and a self-answered post on our beloved developerforce boards about rows having no value for date fields (tried removing all date fields). Does anyone have an idea, or a well placed smack on the head?

@isTest
private class AggregateRefreshTest {

//TODO: increase AggregateRefresh unit tests

//this is just a basic happy path run to start coverage
static testMethod void myUnitTest() {

    //added in the example code from apex reference to check sanity
    //initially was having the same issues; works now -
    //but i have no idea what i did to the csv file
    List<sObject> ls = Test.loadData(Account.sObjectType, 'testAccounts');
    System.assert(ls.size() == 3);


    //always fails at this statement
    List<sObject> td = Test.loadData(Lead.sObjectType, 'testLeads200');
    //verify loaded
    system.assert(false);  //another sanity check
    System.assertEquals(200, td.size(), 'Static Resource Test Data did not load');

    ID batchprocessid; Integer batch= 200; String fld='Division';
    String[] vals = new String[]{};

    for (sObject l : td){
        vals.add(String.valueOf(l.get('Division')));
    }   //for (sObject l : td)

    Test.startTest();
        batchprocessid = Database.executeBatch(new AggregateRefresh(fld, vals), batch);
    Test.stopTest();

    AsyncApexJob a = [SELECT Id, Status, NumberOfErrors, JobItemsProcessed, TotalJobItems, 
        CreatedBy.Email, CompletedDate, CreatedDate FROM AsyncApexJob WHERE Id = :batchprocessid];

    System.assertEquals('Completed', a.Status, 'AsyncApexJob Status');

}   //static testMethod void myUnitTest()

}   //private class AggregateRefreshTest

Best Answer

As I remember, I discovered 3 details about the file to get this working. Ended up with 120 records in 14KB, UTF-8 without BOM file uploaded as txt/csv in Static Resources.

  1. For "System.UnexpectedException" error, the size had to be <20k. I reduced the number of records to 120, making it ~14K in size.

  2. "System.StringException: Unknown field:" - parser was reading the BOM; saved file without. That is probably what happened for line 11 (in the posted code). //but i have no idea what i did to the csv file

  3. "System.StringException: CSV Parse error:" - yep, found one buried in the data; there are a LOT of fields in these records (don't ask).

still waiting for that well placed smack on the head

Related Topic