0

I am trying to improve my test coverage for my test class and for that I want to call a particular method with if conditions in it and I am not sure how to call that in my test class. Below is my method I want to have in my test class.

/**
 * This method finds the related reps based on the territoryID 
 */
public static Shipping_Address__c FindTerritoryReps(Shipping_Address__c a, Id matchingTerritoryObjectID, Map<id,territory__c> SalesTerritoryMappings) { //returns Id of pricebook
    //Get only the matching sales territory mappings
    List<Sales_Territory_Mapping__c> salesRepMappings = SalesTerritoryMappings.get(matchingTerritoryObjectID).Sales_Territory_Mapping__r;
    String RepBasedTerritoryNames = null;
    for(Sales_Territory_Mapping__c salesRepMapping : salesRepMappings){
        //system.debug('TEST SalesTerrMName__c '+ salesRepMapping.SalesTerrMName__c);
        if(salesRepMapping.Role__c=='FSALM'){
            a.Sales_Rep_M_Name__c = salesRepMapping.Sales_Rep__c;
            a.AccTerritoryNameM__c = salesRepMapping.SalesTerrMapName__c;
        }

        if(salesRepMapping.Role__c=='FSALCS'){
            a.Sales_Rep_CS_Name__c = salesRepMapping.Sales_Rep__c;
            a.AccTerritoryNameCS__c = salesRepMapping.SalesTerrMapName__c;
        }           

        if(salesRepMapping.Role__c=='ISALCS'){
            a.ISAL_CS_ID__c = salesRepMapping.Sales_Rep__c;
        }

        if(salesRepMapping.Role__c=='ISALM'){
            a.ISAL_M_ID__c = salesRepMapping.Sales_Rep__c;           
        }

        if(salesRepMapping.Role__c=='Sales Manager M'){
            a.Sales_Manager_M_ID__c = salesRepMapping.Sales_Rep__c;           
        }

        if(salesRepMapping.Role__c=='Sales Manager CS'){
            a.Sales_Manager_CS_ID__c = salesRepMapping.Sales_Rep__c;           
        }            

        if(salesRepMapping.Role__c=='ISAL ManagerM'){
            a.ISAL_Manager_M_ID__c = salesRepMapping.Sales_Rep__c;           
        }

        if(salesRepMapping.Role__c=='ISAL Manager CS'){
            a.ISAL_Manager_CS_ID__c = salesRepMapping.Sales_Rep__c;         
        }                                                

        /*
        if(salesRepMapping.Account_Owner__c == true){
            a.OwnerId = salesRepMapping.Sales_Rep__c;
        }
        */
        a.Territory__c = RepBasedTerritoryNames;
    }
    system.debug('TEST terrnames '+ a.TerritoryNames__c);
    return a; 

 /**
 * This method finds the related SoldinCountry based on the territoryID 
 */        
}
}
1
  • Can you post your test class whatever you have tried and what specific issues you are facing Commented Feb 11, 2017 at 1:32

2 Answers 2

0

You do it just how to would call any method, build your data and pass it into your method. So build a Shipping_Address__c test record, that ID, and a Map<ID, Territory__c> so something like

Shipping_Address__c shippingAddressResult = YourClassName.FindTerritoryReps(testShippingAddress, matchingTerritoryObjectID, salesTerritoryMappings)

And then you can assert your shippingAddressResult

0

Recently I invented some test generation framework. However, it became too complex. I was thinking about to open source simplified version of this framework to see community feedback. I believe this framework might help you to write test very quickly without too much efforts. Just look.

This is my framework

/**
 * @author: Bohdan Dovhan a.k.a. Patlatus
 * (c) All Rights Reserved, 2017
 */
@isTest
public class TestGen {
/**
 * Method to generate object, just call like this:
 *   TestGen.generate('Account:Name=Test Account Name', true)
 */
public static List<SObject> generate( String generator, Boolean doInsert ) {
    return generate( generator, 1, doInsert );
}
/**
 * Method to generate object, just call like this:
 *   TestGen.generate('Account:Name=Test Account Name', 2, true)
 */
public static List<SObject> generate( String generator, Integer multiplicationFactor, Boolean doInsert ) {
    String sObjectName = generator.substringBefore(':');
    String definition = generator.substringAfter(':');


    Type t = Type.forName( sObjectName );
    System.assertNotEquals( null, t, sObjectName + ' is not a valid type' );
    List<SObject> records = new List<SObject>();
    for ( Integer i = 0; i < multiplicationFactor; i++ ) {
        records.add( (SObject) t.newInstance() );
    }
    if ( !String.isBlank( definition ) ) {
        records = processDefinition( records, definition );
    }
    if ( doInsert )
        insert records;
    return records;
}

static List<SObject> processDefinition( List<SObject> records, String definition ) {
    List<String> pairs = definition.split(',');
    for ( String pair: pairs ) {
        String field = pair.substringBefore('=');
        String valueDef = pair.substringAfter('=');
        Boolean multipleValue = valueDef.contains('|') ;
        if ( multipleValue )
            records = multiply( records, field, valueDef );
        else 
            setFieldsValue( records, field, valueDef );
    }
    return records;
}
static List<SObject> multiply( List<SObject> toReturn, String fieldsDefinition, String def ) {
    System.assert(toReturn.size() >= 1, 'No sense to pass empty list here');
    String sObjectType = toReturn[0].getSobjectType().getDescribe().getName();
    return multiply( toReturn, fieldsDefinition, parseValuesDef( sObjectType, def ) );
}

static List<SObject> multiply( List<SObject> toReturn, String fieldsDefinition, Set<Object> values ) {
    List<SObject> newList = new List<SObject>();
    for ( SObject r: toReturn ) {
        for ( Object v: values ) {
            SObject record = r.clone(true, true, true, true);
            for ( String field: fieldsDefinition.split('\\|') ) {
                record.put( field, v );
            }
            newList.add( record );
        }
    }
    return newList;
}
static List<SObject> setFieldsValue( List<SObject> records, String fieldsDefinition, Object value ) {
    for ( SObject r: records ) {
        for ( String field: fieldsDefinition.split('\\|') ) {
            r.put( field, value );
        }
    }
    return records;
}

static Set<Object> parseValuesDef( String sObjectName, String def ) {
    String[] items = def.split('\\|');
    Set<Object> toReturn = new Set<Object>();
    for ( String item: items ) {
        toReturn.add( item );
    }     
    return toReturn;
}
}

and this is how your test class might look like if your class is called CLS

@isTest
private class TestCLS {
static String roles = 'FSALM|FSALCS|ISALCS|ISALM|Sales Manager M|Sales Manager CS|ISAL ManagerM|ISAL Manager CS';
static testMethod void testFindTerritoryReps() {
    SObject shippingAddr = TestGen.generate('Shipping_Address__c:', true)[0];
    SObject territory = TestGen.generate('Territory__c:', true)[0];
    System.debug(LoggingLevel.ERROR, '@@@ territory.Id: ' + territory.Id );
    SObject[] x = TestGen.generate(
        'Sales_Territory_Mapping__c:Territory__c=' + territory.Id +
        ',Role__c=' + roles
        , true);
    System.debug(LoggingLevel.ERROR, '@@@ x: ' + x );
    Map<Id,Territory__c> territoryMap = new Map<Id,Territory__c>(
        [
            SELECT Id, 
            ( SELECT Role__c, Sales_Rep__c, SalesTerrMapName__c FROM Sales_Territory_Mapping__r )
            FROM Territory__c
        ]
    );
    System.debug(LoggingLevel.ERROR, '@@@ territoryMap: ' + territoryMap );
    Test.startTest();
       CLS.FindTerritoryReps((Shipping_Address__c) shippingAddr, territory.Id, territoryMap);
    Test.stopTest();
    /**
    Do not forget to have some good asserts here
    */
}
}
2
  • Hi Patlatus, thank you for the framework. I want to know more about this framework. Is there any article or blog where you have detailed description about this framework.
    – user28497
    Commented Feb 14, 2017 at 19:50
  • @Ram: yes, you can find general description of it here: patlatus.wordpress.com/2017/02/01/…
    – Patlatus
    Commented Feb 15, 2017 at 9:32

You must log in to answer this question.