0

I am creating a testdatafactory. creating a country goes well. When creating accounts my country fields are null if i debug CountryList it returns the created country record.

Factory class

@isTest
public class TestDataMaker{

    private List<Country__c> createdCountries = new List<Country__c>();
    public List<Country__c> createCountries(Integer num){      
        List<Country__c> results = new List<Country__c>();
        for(Integer i = 0; i <= num; i++){
            Country__c c;
            if(this.createdCountries.size() > i){
                c = this.createdCountries[i];
            }else{
                c = new Country__c(Name  = 'Nederland'+'-'+i , ISO2Code__c  = 'NL' , IBANLength__c  = 18, ISO3Code__c = 'NLD' , CountryNumber__c =151);
                this.createdCountries.add(c);
            }
            results.add(c);
        }
        return results;
    }


    private List<Account> createdAccounts = new List<Account>();
    public List<Account> createAccounts(Integer num){
        List<Country__c> CountryList = createCountries(num); 
        system.debug('call countries '+CountryList);
        List<Account> results = new List<Account>();
        for(Integer i = 0; i <= num; i++){
            Account acc;
            if(this.createdAccounts.size() > i){
                acc = this.createdAccounts[i];
            }else{
                acc = new Account(Name='TestAccount' +'-'+ i , Country__c = CountryList[i].id);
                this.createdAccounts.add(acc);
            }
            results.add(acc);
        }
        return results;
    }
}

Test

@isTest
public class TestDataFactoryTest {

    @isTest static void createCountries(){
     TestDataMaker obj = new TestDataMaker();  
    system.debug('TestDataMaker ' + obj.createCountries(5));
    }

    @isTest static void createAccounts(){
     TestDataMaker obj = new TestDataMaker();  
    system.debug('TestDataMaker ' + obj.createAccounts(5));
}
}
0

As far as I can see, you are not inserting the countries that you have constructed in TestDataMaker.createCountries() method. You just create the records, add to those list but never perform the DML operation.

Becuase you never inserted the countries, so in your TestDataMaker.createAccounts() method, you won't get the Country Id on this line:

acc = new Account(Name='TestAccount' +'-'+ i , Country__c = CountryList[i].id);

as there was no Id generated for the country records and thus CountryList[i].id would return null.

To fix this, you would need to make sure that you insert the country records to be able to utilize their id in your other method when you create the account.

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.