1

I have created a generic sorting utility using the comparable interface:

public class GenericSortingUtil {

public virtual class ObjWrapper implements Comparable {

    public sObject obj;

    public ObjWrapper() {}

    public ObjWrapper(Sobject obj) {
        this.obj = obj;
    }

    public virtual Integer compareTo(Object compareTo) {

        ObjWrapper compareToObj = (ObjWrapper)compareTo;

        if((String)obj.get('Product_Group_Name__c') > (String)compareToObj.obj.get('Product_Group_Name__c')) {
            return 1;
        }

        if((String)obj.get('Product_Group_Name__c') == (String)compareToObj.obj.get('Product_Group_Name__c')) {
            return 0;
        }

        return -1;
    }
}     
}

I created a child class that extends the generic sorting utility:

public class ShirtWrapper extends GenericSortingUtil.ObjWrapper {

public ShirtWrapper() {}

public ShirtWrapper(Sobject obj) {
    super(obj);
}   

public override Integer compareTo(Object compareTo) {         
    ShirtWrapper compareToObj = (ShirtWrapper)compareTo;
    integer nameOrder = super.compareTo(compareTo);
    return nameOrder == 0 ? getSizeOrder(String.valueOf(obj.get('Size__c'))) - getSizeOrder(String.valueOf(compareToObj.obj.get('Size__c'))) : nameOrder;
}   


public Integer getSizeOrder(string size) {
    Integer retval = -1;
    switch on size {
        when 'XS' {
            retval = 0;
        } 
        when 'SM' {
            retval = 1;
        }
        when 'MD' {
            retval = 2;
        }
        when 'LG' {
            retval= 3;
        }
        when 'XL' {
            retval = 4;
        }
        when '2XL' {
            retval = 5;
        }
        when '3XL' {
            retval = 6;
        }
        when '4XL' {
            retval = 7;
        }
        when else {
            return retval;
        }
    }
    return retval;
} 
}

I'm trying to cover the classes in a unit test. I was looking at the documentation for comparable and the example provided for unit testing:

@isTest
private class ShirtWrapperTest {

static Product2 testProduct1;
static List<ShirtWrapper> wrapperList;

static void initialize() {

    wrapperList = new List<ShirtWrapper>();

    testProduct1 = TestFactory.createTestProduct('Widget', Schema.SObjectType.Product2.getRecordTypeInfosByName().get('Blank Product').getRecordTypeId());
    testProduct1.Unit_Cost__c = 4.61;
    testProduct1.Inbound_Shipping_Cost__c = 0.0;
    testProduct1.Product_Group_Name__c = 'Widget';
    wrapperList.add(new ShirtWrapper(testProduct1));        
}

static testMethod void testShirtWrapperSorting() {
    initialize();

    test.startTest();
        wrapperList.sort();
    test.stopTest();

}
}

When I call the sort method, it never hits the compareTo method. The constructor fires in both utilities, but it stops there.

1 Answer 1

2

You need at least two items in order to perform a sort. Preferably you'll want at least 3 different items, where item 1 is the highest value, item 2 is the lowest value, and item 3 is the the middle value. This will cover all three branches of determining which value is highest. For the one with the major switch statement, you'd have to test each possible value at least once. Also, since you're using when else, I suspect the final return retVal is not necessary; use either when else, or use the final return retVal, otherwise you can't accomplish 100% coverage.

1
  • thank you @sfdcfox I was able to get the classes covered. I appreciate the help!
    – Dman100
    Jun 19, 2018 at 21:31

You must log in to answer this question.

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