0

I have the following method

public static String splitCompanyName(String company) {
    String companyNameLowerCase = company.toLowerCase();
    // get all extensions from the salesforce list and are added in the set
    List<Company_Extensions__c> extensions = Company_Extensions__c.getAll().values();
    Set<String> companyExtensions = new Set<String>();
    for(Company_Extensions__c extension : extensions) {
        companyExtensions.add(extension.Name);                
    }
    // remove all extensions from company name
    for(String companyExtension : companyExtensions){
        integer extIndex = companyNameLowerCase.indexOf(' ' + companyExtension);
        if(extIndex > -1) {
            // in order to avoid IndexOutOfBounds exception the two variables must be with equal values regardless of the case
            company = company.substring(0, extIndex);
            companyNameLowerCase = companyNameLowerCase.substring(0, extIndex);    
        }
    }        
    return company;        
}

When I run test from a test class

static testMethod void testSplitCompanyName(){
     Test.startTest();
     String company = 'Salesforce Org';
     LeadToMerchantController.splitCompanyName(company);            
     Test.stopTest();
}

for some reason List extensions does not contain elements because it does not enter the first for loop.

Here are the details from the log:enter image description here

Could please advise how to get the values of the emlements from the list.

enter image description here

  • you need to create test data in test class .. create Company_Extensions__c data – Ratan Paul Jan 7 '16 at 16:42
1

If you want to access data that already exists in your organization, you can add (seeAllData=true) to your @isTest annotation:

@isTest(SeeAllData=true)
static testMethod void testSplitCompanyName(){
...

Tests run without the ability to access your data for security reasons and testing best practices. If you are trying to achieve required test coverage and create a test suite for your code, you should create data for your tests that not only serve your assertions, but also provide additional chances to verify your code handles erroneous data as well.

For more information on creating data for your test cases, go here.

  • You've got to be kidding -- this is not best practice at all -it leads to deployment issues when sandboxes don't map to Prod; the OP's custom setting should be mocked in the test method as you noted in Ratan's answer – cropredy Jan 7 '16 at 21:39
  • OP didn't ask for best practice. He asked why his code didn't work the way it was laid out. The issue is there is no data. OP's code expects data to already exist and this expectation is furthered by the fact that OP posted data from his Org and specifically asked what the disconnect was. I mention that he should be creating mock data as a courtesy, in case he wants to use these tests later for test coverage, but to lay out how he should setup his mock data (w.r.t best practices, as well) is beyond the scope of his question. – JeffyB Jan 7 '16 at 22:05
  • understand your point and we can 'agree to disagree'; my point being that this community has taken great care through dozens/hundreds of responses to steer OPs away from seeAllData=true; just like the Community always tells OPs to bulkify even when they could technically get away from not bulkifying – cropredy Jan 7 '16 at 23:22
  • As a compromise, I've offered that this can solve the user's main issue, but also suggested best practices and linked to SF documentation on how to create test data. – JeffyB Jan 8 '16 at 15:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.