1

I'm trying to check a custom setting, containing a list of Record Type Ids against the record type id of new opportunities that are being created. I only want the trigger to complete it's calculation, if there is a match.

When I run my test class, I don't have code coverage for the parts of the code which depend on a match having been found between the opportunity Record Type Id and the Id included in the Custom Setting list. So I'm assuming that when the two are being compared, there isn't a match but I can't figure out why there wouldn't be.

List<WEID__c> weids = WEID__c.getall().values();
Set<Id> validRecordTypeIds = new Set<Id>();

//check that Ids listed in custom setting are valid Ids
for (WEID__c weid : weids) {
    try {
        validRecordTypeIds.add(weid.WEOppId__c);
    } catch (System.StringException e) {
        System.debug(
            System.LoggingLevel.ERROR,
            'Invalid record type id ' + weid.WEOppId__c
        );
    }
}

for (Opportunity opp : Trigger.new) {
    if (validRecordTypeIds.contains(opp.Id)) {

        if (opp.Annual_Settlement_Value__c > 0 && opp.Interchange_Rate_Revenue_as_a_percentag__c > 0) {
            Decimal fsr = opp.Annual_Settlement_Value__c * opp.Interchange_Rate_Revenue_as_a_percentag__c;

            opp.FSR_Estimate_on_Contract_Signature__c = fsr;

        } else {
            opp.Annual_Settlement_Value__c = null;
            opp.FSR_Estimate_on_Contract_Signature__c = null;
        }
    }
}

Part of my test class which specified a Record Type Id (I'm happy to post the rest if necessary, I just wanted to keep this post short) -

static testMethod void testFSR() {

    Opportunity opp = new Opportunity();
    opp.name = 'Test Opp';
    opp.RecordTypeId = '012700000005qif';
    opp.StageName = '1) Suspect';
    opp.CloseDate = date.newInstance(2045, 01, 01);


    //cover Credit Agreement Details Required validation rule
    opp.Credit_Limit__c = 100.00;
    opp.Date_Credit_Limit_Approved__c = date.newInstance(1901, 01, 01);
    opp.Payment_Terms_Days__c = 5;
    opp.Billing_Cycle__c = 'Daily';

    //cover Revenue Values Required validation rule (and provide values required for calculation)
    opp.Annual_Settlement_Value__c = 100.00;
    opp.Average_transaction_Value_POS__c = 1.00;

    opp.Interchange_Rate_Revenue_as_a_percentag__c = 0.1;

    insert opp;

}

I do have another section of the test class that specifies a non-matching Id. My custom setting WEID__c contains the field WEOppID__c and one of the values is 012700000005qif.

2
  • Alex - custom settings values need to be mocked in testmethod as they are not available with annotation @isTest - which I'm presuming you are using
    – cropredy
    Commented Feb 21, 2015 at 17:23
  • @crop1645 I've not come across this before (this is my second attempt at writing code), let me look into that. Yes I was using "@isTest".
    – Alex S
    Commented Feb 21, 2015 at 17:24

1 Answer 1

3

When using @isTest annotation, custom settings values won't be available in the testmethod

Instead, you need to mock those values in the initial part of the test method, just like you would any other sObject.

More likely than not, if you have a moderate to large system, you should put your custom settings mocks into a utility testfactory class as you'll be mocking them over and over again otherwise..

Avoid the temptation to use seeAllData=true annotation as this can lead to deployment issues days or even months later.

2
  • Ok that hasn't completely solved my issue, I still don't have code coverage for the 'if values > 0' checks but it's covered the try{ validRecordTypeIds.add...so it must have worked. Thanks a lot!
    – Alex S
    Commented Feb 21, 2015 at 18:05
  • In fact I've just tried using seeAllData=true instead of static testMethod void testThisThing() { WEID__c cs = new WEID__c(); cs.Name = 'Test Custom Setting'; cs.WEOppID__c = '012700000005qif'; insert cs; and got the same code coverage so it definitely worked.
    – Alex S
    Commented Feb 21, 2015 at 18:19

You must log in to answer this question.

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