+ Start a Discussion
AmberTaylorAmberTaylor 

Test class for Switch Condition Stuck to 68%

Hi there, I have written a Test Class for a Switch condition but When I'm executing Tests, I still have a 68% Coverage, nothing more. 

Here is my main class wich is tested until case : 33 
public with sharing class UpdateDepartmentField_CLASS {
    public static void UpdateDepartmentFieldCommune(List<Commune__c> communeList) {

        for (Integer i=0;  i < communeList.size(); i++) {
                switch on communeList[i].CodeDepartement__c {
                    when '01' {
                        communeList[i].Departement__c = '01 AIN';
                    }
                    when '02' {
                        communeList[i].Departement__c = '02 AISNE';
                    }
                    when '03' {
                        communeList[i].Departement__c = '03 ALLIER';
                    }
                    when '04' {
                        communeList[i].Departement__c = '04 ALPES-DE-HAUTE-PROVENCE';
                    }
                    when '05' {
                        communeList[i].Departement__c = '05 HAUTES-ALPES';
                    }
                    when '06' {
                        communeList[i].Departement__c = '06 ALPES-MARITIMES';
                    }
                    when '07' {
                        communeList[i].Departement__c = '07 ARDECHE';
                    }
                    when '08' {
                        communeList[i].Departement__c = '08 ARDENNES';
                    }
                    when '09' {
                        communeList[i].Departement__c = '09 ARIEGE';
                    }
                    when '10' {
                        communeList[i].Departement__c = '10 AUBE';
                    }
                    when '11' {
                        communeList[i].Departement__c = '11 AUDE';
                    }
                    when '12' {
                        communeList[i].Departement__c = '12 AVEYRON';
                    }
                    when '13' {
                        communeList[i].Departement__c = '13 BOUCHES-DU-RHONE';
                    }
                    when '14' {
                        communeList[i].Departement__c = '14 CALVADOS';
                    }
                    when '15' {
                        communeList[i].Departement__c = '15 CANTAL';
                    }
                    when '16' {
                        communeList[i].Departement__c = '16 CHARENTE';
                    }
                    when '17' {
                        communeList[i].Departement__c = '17 CHARENTE-MARITIME';
                    }
                    when '18' {
                        communeList[i].Departement__c = '18 CHER';
                    }
                    when '19' {
                        communeList[i].Departement__c = '19 CORREZE';
                    }
                    when '21' {
                        communeList[i].Departement__c = '21 COTE-D\'OR';
                    }
                    when '22' {
                        communeList[i].Departement__c = '22 COTES-D\'ARMOR';
                    }
                    when '23' {
                        communeList[i].Departement__c = '23 CREUSE';
                    }
                    when '24' {
                        communeList[i].Departement__c = '24 DORDOGNE';
                    }
                    when '25' {
                        communeList[i].Departement__c = '25 DOUBS';
                    }
                    when '26' {
                        communeList[i].Departement__c = '26 DROME';
                    }
                    when '27' {
                        communeList[i].Departement__c = '27 EURE';
                    }
                    when '28' {
                        communeList[i].Departement__c = '28 EURE-ET-LOIR';
                    }
                    when '29' {
                        communeList[i].Departement__c = '29 FINISTERE';
                    }
                    when '30' {
                        communeList[i].Departement__c = '30 GARD';
                    }
                    when '31' {
                        communeList[i].Departement__c = '31 HAUTE-GARONNE';
                    }
                    when '32' {
                        communeList[i].Departement__c = '32 GERS';
                    }
                    when '33' {
                        communeList[i].Departement__c = '33 GIRONDE';
                    }
                    when '34' {
                        communeList[i].Departement__c = '34 HERAULT';
                    }
                    when '35' {
                        communeList[i].Departement__c = '35 ILLE-ET-VILAINE';
                    }
                    when '36' {
                        communeList[i].Departement__c = '36 INDRE';
                    }
                    when '37' {
                        communeList[i].Departement__c = '37 INDRE-ET-LOIRE';
                    }
                    when '38' {
                        communeList[i].Departement__c = '38 ISERE';
                    }
                    when '39' {
                        communeList[i].Departement__c = '39 JURA';
                    }
                    when '40' {
                        communeList[i].Departement__c = '40 LANDES';
                    }
                    when '41' {
                        communeList[i].Departement__c = '41 LOIR-ET-CHER';
                    }
                    when '42' {
                        communeList[i].Departement__c = '42 LOIRE';
                    }
                    when '43' {
                        communeList[i].Departement__c = '43 HAUTE-LOIRE';
                    }
                    when '44' {
                        communeList[i].Departement__c = '44 LOIRE-ATLANTIQUE';
                    }
                    when '45' {
                        communeList[i].Departement__c = '45 LOIRET';
                    }
                    when '46' {
                        communeList[i].Departement__c = '46 LOT';
                    }
                    when '47' {
                        communeList[i].Departement__c = '47 LOT-ET-GARONNE';
                    }
                    when '48' {
                        communeList[i].Departement__c = '48 LOZERE';
                    }
                    when '49' {
                        communeList[i].Departement__c = '49 MAINE-ET-LOIRE';
                    }
                    when '50' {
                        communeList[i].Departement__c = '50 MANCHE';
                    }
                    when '51' {
                        communeList[i].Departement__c = '51 MARNE';
                    }
                    when '52' {
                        communeList[i].Departement__c = '52 HAUTE-MARNE';
                    }
                    when '53' {
                        communeList[i].Departement__c = '53 MAYENNE';
                    }
                    when '54' {
                        communeList[i].Departement__c = '54 MEURTHE-ET-MOSELLE';
                    }
                    when '55' {
                        communeList[i].Departement__c = '55 MEUSE';
                    }
                    when '56' {
                        communeList[i].Departement__c = '56 MORBIHAN';
                    }
                    when '57' {
                        communeList[i].Departement__c = '57 MOSELLE';
                    }
                    when '58' {
                        communeList[i].Departement__c = '58 NIEVRE';
                    }
                    when '59' {
                        communeList[i].Departement__c = '59 NORD';
                    }
                    when '60' {
                        communeList[i].Departement__c = '60 OISE';
                    }
                    when '61' {
                        communeList[i].Departement__c = '61 ORNE';
                    }
                    when '62' {
                        communeList[i].Departement__c = '62 PAS-DE-CALAIS';
                    }
                    when '63' {
                        communeList[i].Departement__c = '63 PUY-DE-DOME';
                    }
                    when '64' {
                        communeList[i].Departement__c = '64 PYRENEES-ATLANTIQUES';
                    }
                    when '65' {
                        communeList[i].Departement__c = '65 HAUTES-PYRENEES';
                    }
                    when '66' {
                        communeList[i].Departement__c = '66 PYRENEES-ORIENTALES';
                    }
                    when '67' {
                        communeList[i].Departement__c = '67 BAS-RHIN';
                    }
                    when '68' {
                        communeList[i].Departement__c = '68 HAUT-RHIN';
                    }
                        when '69' {
                            communeList[i].Departement__c = '69 RHONE';
                        }
                        when '70' {
                            communeList[i].Departement__c = '70 HAUTE-SAONE';
                        }
                        when '71' {
                            communeList[i].Departement__c = '71 SAONE-ET-LOIRE';
                        }
                        when '72' {
                            communeList[i].Departement__c = '72 SARTHE';
                        }
                        when '73' {
                            communeList[i].Departement__c = '73 SAVOIE';
                        }
                        when '74' {
                            communeList[i].Departement__c = '74 HAUTE-SAVOIE';
                        }
                        when '75' {
                            communeList[i].Departement__c = '75 PARIS';
                        }
                        when '76' {
                            communeList[i].Departement__c = '76 SEINE-MARITIME';
                        }
                        when '77' {
                            communeList[i].Departement__c = '77 SEINE-ET-MARNE';
                        }
                        when '78' {
                            communeList[i].Departement__c = '78 YVELINES';
                        }
                        when '79' {
                            communeList[i].Departement__c = '79 DEUX-SEVRES';
                        }
                        when '80' {
                            communeList[i].Departement__c = '80 SOMME';
                        }
                        when '81' {
                            communeList[i].Departement__c = '81 TARN';
                        }
                        when '82' {
                            communeList[i].Departement__c = '82 TARN-ET-GARONNE';
                        }
                        when '83' {
                            communeList[i].Departement__c = '83 VAR';
                        }
                        when '84' {
                            communeList[i].Departement__c = '84 VAUCLUSE';
                        }
                        when '85' {
                            communeList[i].Departement__c = '85 VENDEE';
                        }
                        when '86' {
                            communeList[i].Departement__c = '86 VIENNE';
                        }
                        when '87' {
                            communeList[i].Departement__c = '87 HAUTE-VIENNE';
                        }
                        when '88' {
                            communeList[i].Departement__c = '88 VOSGES';
                        }
                        when '89' {
                            communeList[i].Departement__c = '89 YONNE';
                        }
                        when '90' {
                            communeList[i].Departement__c = '90 TERRITOIRE DE BELFORT';
                        }
                        when '91' {
                            communeList[i].Departement__c = '91 ESSONNE';
                        }
                        when '92' {
                            communeList[i].Departement__c = '92 HAUTS-DE-SEINE';
                        }
                        when '93' {
                            communeList[i].Departement__c = '93 SEINE-SAINT-DENIS';
                        }
                        when '94' {
                            communeList[i].Departement__c = '94 VAL-DE-MARNE';
                        }
                        when '95' {
                            communeList[i].Departement__c = '95 VAL-D\'OISE';
                        }
                        when '2B', '2b' {
                            communeList[i].Departement__c = '2B Haute-Corse ';
                        }
                        when '2A', '2a' {
                            communeList[i].Departement__c = '2A Corse-du-Sud ';
                        }
                        when else {
                            communeList[i].Departement__c = null;
                        }
                    }
                }
            }
 }
I know it's a huge condition so maybe there are some limitations ?

Here is my test class
@isTest
public with sharing class UpdateDepartmentField_TEST {
    @isTest static void TestUpdateDepartmentField() {
        //First I create a commune that should be automatically updated with Department__c
        Commune__c newCommune = new Commune__c();
        newCommune.Name='Commune 1';
        newCommune.CodeInseeCommune__c='12265';
        newCommune.CodeDepartement__c='01';
        newCommune.CodeCanton__c='02';
        newCommune.CodeRegion__c='73';
        insert newCommune;

        //Then i'll update this commune with all dept
        for (Integer i=2; i < 96; i++) {
            if (i<10) {
                newCommune.CodeDepartement__c= '0'+String.valueOf(i);
                update newCommune;
            } else if (i>9 && i<96) {
                newCommune.CodeDepartement__c= String.valueOf(i);
                update newCommune;
            }
        }

        newCommune.CodeDepartement__c='2B';
        update newCommune;

        newCommune.CodeDepartement__c='2A';
        update newCommune;         
    }
}
I don't know what i'm missing here.
When I test manually everithing seems fine, so i think it's only related to the test class. 
Do you see anything that could help reach 100% for this test class ? 

 
Best Answer chosen by AmberTaylor
TechingCrewMattTechingCrewMatt
Is there a try/catch in the trigger that calls the method? I think you might be hitting DML limits. The following example is more efficient and tests the limits when 96 records are updated at once:
 
@isTest
public with sharing class UpdateDepartmentField_TEST {
    @isTest static void TestUpdateDepartmentField() {
        //add all communes to list to be inserted so they can be updated in one transaction
        List<Commune__c> communes = new List<Commune__c>();
        for(Integer i=2; i < 96; i++){
            String departmentCode = i < 10 ? '0' + String.valueOf(i) : String.valueOf(i);
            communes.add(
                new Commune__c(
                    Name='Commune ' + departmentCode,
                    CodeInseeCommune__c='12265',
                    CodeDepartement__c='01',
                    CodeCanton__c='02',
                    CodeRegion__c='73'
                )
            );
        }
        insert communes;
        communes = new List<Commune__c>([SELECT Id, Name FROM Commune__c]);
        for(Commune__c commune : communes){
            //assign the code department
            commune.CodeDepartment__c = commune.Name.split(' ')[1];
        }
        Test.startTest();
        //test updating 96 communes at once
        update communes;
        Test.stopTest();
        
        //test the last two scenarios
        communes = new List<Commune__c>([SELECT Id, Name FROM Commune__c LIMIT 2]);
        communes[0].CodeDepartement__c='2A';
        communes[1].CodeDepartement__c='2B';
        update communes;        
    }
}

I couldn't atualy test it without creating the object, but it should be a good starting point.

All Answers

TechingCrewMattTechingCrewMatt
Is there a try/catch in the trigger that calls the method? I think you might be hitting DML limits. The following example is more efficient and tests the limits when 96 records are updated at once:
 
@isTest
public with sharing class UpdateDepartmentField_TEST {
    @isTest static void TestUpdateDepartmentField() {
        //add all communes to list to be inserted so they can be updated in one transaction
        List<Commune__c> communes = new List<Commune__c>();
        for(Integer i=2; i < 96; i++){
            String departmentCode = i < 10 ? '0' + String.valueOf(i) : String.valueOf(i);
            communes.add(
                new Commune__c(
                    Name='Commune ' + departmentCode,
                    CodeInseeCommune__c='12265',
                    CodeDepartement__c='01',
                    CodeCanton__c='02',
                    CodeRegion__c='73'
                )
            );
        }
        insert communes;
        communes = new List<Commune__c>([SELECT Id, Name FROM Commune__c]);
        for(Commune__c commune : communes){
            //assign the code department
            commune.CodeDepartment__c = commune.Name.split(' ')[1];
        }
        Test.startTest();
        //test updating 96 communes at once
        update communes;
        Test.stopTest();
        
        //test the last two scenarios
        communes = new List<Commune__c>([SELECT Id, Name FROM Commune__c LIMIT 2]);
        communes[0].CodeDepartement__c='2A';
        communes[1].CodeDepartement__c='2B';
        update communes;        
    }
}

I couldn't atualy test it without creating the object, but it should be a good starting point.
This was selected as the best answer
AmberTaylorAmberTaylor
Hi Matt, 
Your precious help solved my problem :) Thanks a lot for your time. 

I don't have a try/catch in the trigger that uses the methob but i'll add it. 
 
@isTest
public with sharing class UpdateDepartmentField_TEST {
    @isTest static void TestUpdateDepartmentField() {
        //add all communes to list to be inserted so they can be updated in one transaction
        List<Commune__c> communes = new List<Commune__c>();

        for (Integer i=2; i < 96; i++) {
            String departmentCode = i<10 ? '0' + String.valueOf(i) : String.valueOf(i);

            communes.add(
                new Commune__c(
                    Name='Commune ' + departmentCode,
                    CodeInseeCommune__c='122'+ departmentCode,
                    CodeDepartement__c='01',
                    CodeCanton__c='02',
                    CodeRegion__c='73'
                )
            );
        }

        insert communes;

        communes = new List<Commune__c>([Select Id, Name FROM Commune__c]);
        for (Commune__c c : communes) {
            // assign code dept
            c.CodeDepartement__c = c.Name.split(' ')[1];
        }

        Test.startTest();
        update communes;
        Test.stopTest();

        //test the last 2 scenarios    
        communes = new List<Commune__c>([SELECT Id, Name, CodeDepartement__c FROM Commune__c LIMIT 2]);
        communes[0].CodeDepartement__c='2B';
        communes[1].CodeDepartement__c='2A';
        update communes;         
    }
}

Here is my updated code. :)