0

Refer to below test class,

@isTest
public with sharing class CategoryHelperTest {

        private static boolean testDataCreated = false;
        private static Scheme__c ss;
        private static Scheme__c ss2;
        private static Scheme__c ss3;
        private static Scheme__c ss4;
        private static Scheme__c ss5;


        private static Band__c sb;
        private static Band__c sb2;
        private static Band__c sb3;

        private static Category__c ic;
        private static Category__c ic2;
        private static Category__c ic3;
        private static Category__c ic4;
        private static Category__c ic5;
        private static Category__c ic6;
        private static Category__c ic7;
        private static Category__c ic8;
        private static Category__c ic9;

        private static Band_Entry__c sbe;
        private static Band_Entry__c sbe2;
        private static Band_Entry__c sbe3;

        private String ErrorMessage;

    public static void tester(){
        ss = new Scheme__c(Name = 'TestScheme', Description__c = 'TestScheme');
        insert ss;
        ss2 = new Scheme__c(Name = 'TestScheme', Description__c = 'TestScheme2');
        insert ss2;
        ss3 = new Scheme__c(Name = 'TestScheme', Description__c = 'TestScheme3');
        insert ss3;
        ss4 = new Scheme__c(Name = 'TestScheme', Description__c = 'TestScheme4');
        insert ss4;
        ss5 = new Scheme__c(Name = 'TestScheme', Description__c = 'TestScheme5');
        insert ss5;
        ss6 = new Scheme__c(Name = 'TestScheme', Description__c = 'TestScheme6');
        insert ss6;
        ss7 = new Scheme__c(Name = 'TestScheme', Description__c = 'TestScheme7');
        insert ss7;
        ss8 = new Scheme__c(Name = 'TestScheme', Description__c = 'TestScheme8');
        insert ss8;
        ss9 = new Scheme__c(Name = 'TestScheme', Description__c = 'TestScheme9');
        insert ss9;
        ss10 = new Scheme__c(Name = 'TestScheme', Description__c = 'TestScheme10');
        insert ss10;

        sb = new Band__c(Name = 'Low', Colour__c = 'Green', Rating__c = 1, Type__c = 'Impact', Scheme__c = ss.id);
        insert sb;
        sb2 = new Band__c(Name = 'Low', Colour__c = 'Green', Rating__c = 1, Type__c = 'Likelihood', Scheme__c = ss.id);
        insert sb2;
        sb3 = new Band__c(Name = 'Medium', Colour__c = 'Amber', Rating__c = 2, Type__c = 'Likelihood', Scheme__c = ss.id);
        insert sb3;

        //Testing with 'Cost' & 'Time' 
        ic = new Category__c(Name = 'HR',Symbol__c = '?', Justification_Required__c='Inherent;Target', Type_of_Risk__c='Cost', Units__c = 'test', Scheme__c = ss.id);
        insert ic;
        ic2 = new Category__c(Name = 'HR',Symbol__c = '?', Justification_Required__c='Inherent;Target', Type_of_Risk__c='Time', Units__c = 'test', Scheme__c = ss.id);
        insert ic2;

        //Testing with 'Cost' & '%Time' 
        ic3 = new Category__c(Name = 'HR',Symbol__c = '?', Justification_Required__c='Inherent;Target', Type_of_Risk__c='Cost', Units__c = 'test', Scheme__c = ss2.id);
        insert ic3;
        ic4 = new Category__c(Name = 'HR',Symbol__c = '?', Justification_Required__c='Inherent;Target', Type_of_Risk__c='%Time', Units__c = 'test', Scheme__c = ss2.id);
        insert ic4;

        //Testing with '%Cost' & 'Time' 
        ic5 = new Category__c(Name = 'HR',Symbol__c = '?', Justification_Required__c='Inherent;Target', Type_of_Risk__c='%Cost', Units__c = 'test', Scheme__c = ss3.id);
        insert ic5;
        ic6 = new Category__c(Name = 'HR',Symbol__c = '?', Justification_Required__c='Inherent;Target', Type_of_Risk__c='Time', Units__c = 'test', Scheme__c = ss3.id);
        insert ic6;

        //Testing with '%Cost' & '%Time' 
        ic7 = new Category__c(Name = 'HR',Symbol__c = '?', Justification_Required__c='Inherent;Target', Type_of_Risk__c='%Cost', Units__c = 'test', Scheme__c = ss4.id);
        insert ic7;
        ic8 = new Category__c(Name = 'HR',Symbol__c = '?', Justification_Required__c='Inherent;Target', Type_of_Risk__c='%Time', Units__c = 'test', Scheme__c = ss4.id);
        insert ic8;


        sbe = new Band_Entry__c(Label__c = 'TestBandEntry', Description__c = 'TestScoreBandEntry', From_Value__c = 10, To_Value__c = 20, Band__c = sb.id, Category__c = ic.id );
        insert sbe;
        sbe2 = new Band_Entry__c(Label__c = 'TestBandEntry2', Description__c = 'TestScoreBandEntry2', From_Value__c = 10, To_Value__c = 20, Band__c = sb2.id, Category__c = ic.id );
        insert sbe2;
        sbe3 = new Band_Entry__c(Label__c = 'TestBandEntry3', Description__c = 'TestScoreBandEntry3', From_Value__c = 20, To_Value__c = 30, Band__c = sb3.id, Category__c = ic.id );
        insert sbe3;

        testDataCreated = true;
    }

In the test method i'm using different ids to check different scenarios, but if I use a list as below, I can't use different ids for each 'TestScheme1', 'TestScheme2' etc to test different scenarios because all entries are in the list.

How can I get this test class written to test scenarios in the same way using a list or a map ?

public static void tester(){
  List<Scheme__c> insertList = new List<Scheme__c>();
  insertList.add(new Scheme__c(Name = 'TestScheme', Description__c = 'TestScheme'));
  insertList.add(new Scheme__c(Name = 'TestScheme', Description__c = 'TestScheme2'));
  insertList.add(new Scheme__c(Name = 'TestScheme', Description__c = 'TestScheme3'));
  insertList.add(new Scheme__c(Name = 'TestScheme', Description__c = 'TestScheme4'));
  insertList.add(new Scheme__c(Name = 'TestScheme', Description__c = 'TestScheme5'));

  insert insertList;

} 

2 Answers 2

1

I tend to do the following - assumption is you are testing a trigger on Scheme__c

List<Scheme__c> schList = new List<Scheme__c> {
                           new Scheme__c(name='00Scheme', description__c='..'),
                           new Scheme__c(name='01Scheme', description__c='..'),
                           new Scheme__c(name='02Scheme', description__c='..')};
insert schList;
List<Scheme__c> schResList = select id, derivedField1__c, relatedObject__r.field2__c, ...
                               from Scheme__c where id IN: schList order by name asc;
System.assertEquals('someexpectedVal',   schResList[0].derivedField1);
System.assertEquals('someexpectedVal',   schResList[1].derivedField1);
System.assertEquals('someexpectedVal',   schResList[2].derivedField1);

// etc.

// use the description__c field to annotate what the test does so you can keep track of 
// the difference between [0], [1], [2] members of the array.

// use the name field to ensure the result list is sorted in a predictable way

It is great you are trying to bulkify your tests - they'll run faster and produce less debug log

1

If you want to keep the named references to each object you can do this:

public static void tester(){
    ss = new Scheme__c(Name = 'TestScheme', Description__c = 'TestScheme');
    ss2 = new Scheme__c(Name = 'TestScheme', Description__c = 'TestScheme2');
    ss3 = new Scheme__c(Name = 'TestScheme', Description__c = 'TestScheme3');
    ...
    insert new Scheme__c[] {ss, ss2, ss3, ...};
    ...
}

or this:

public static void tester(){
    List<Scheme__c> schemes = new List<Scheme__c>{
            new Scheme__c(Name = 'TestScheme', Description__c = 'TestScheme'),
            new Scheme__c(Name = 'TestScheme', Description__c = 'TestScheme2'),
            new Scheme__c(Name = 'TestScheme', Description__c = 'TestScheme3'),
            ...
            };
    insert schemes;

    // Name the indexes
    Integer ss= 0;
    Integer ss2 = 1;
    Integer ss3 = 2;

    // Reference e.g. "TestScheme2" using "schemes[ss2]"
} 

You must log in to answer this question.

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