function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Marry SteinMarry Stein 

Cover Switch Statement

Hey Guys,

i dont know why, but i am not able to cover the following switch statment.
public void createPdfSections(){
        List<OpportunityLineItem> oliList = [Select Id, Name, ListPrice, TotalPrice,  Quantity,  Product2.CustomQuoteCategory__c,      
                                            ON_Produktbeschreibung_detailliert__c, Produkt__c, CustomQuoteQuantity__c,Custom_Quote_List_Price__c,
                                            Product2.family, Product2.domain__c, Product2.size__c, Product2.Subname__c,SumListPrice__c
                                            FROM OpportunityLineItem
                                            WHERE OpportunityId =:opportunityId Limit 50];

        for(OpportunityLineItem oli : oliList){
            switch on oli.Product2.CustomQuoteCategory__c { // add the prdocuts to different sections for the vf template
                when 'BAS' {
                    sectionFlat.add(oli);
                }
                when 'EMP' {
                    sectionEmployerBranding.add(oli);  
                }
                when 'PER'{
                    sectionPerformance.add(oli);  
                }
                when 'SER'{
                    sectionService.add(oli);  
                }
                when else {
                    sectionSonstiges.add(oli);
                }
            }
        }
    }
Thats my test code : 
@isTest
public class TestCreateCustomOpportunityQuote {  
    @isTest private static void TestCreateCustomOpportunityQuote(){
   
         Opportunity opp = new Opportunity ();
         opp.name = 'test';
         opp.StageName = '40 - Demo Termin vereinbart';
         opp.CloseDate = System.today();
         insert opp;
        
         PageReference testPage = Page.CustomizeOpportunityQuote;
         testPage.getParameters().put('id', opp.id);
         Test.setCurrentPage(testPage);
         ApexPages.StandardController sc = new ApexPages.StandardController(opp);
         CreateCustomOpporutnityQuote tstCtrl = new CreateCustomOpporutnityQuote(sc);
         tstCtrl.opportunityId = testPage.getParameters().get(opp.id);
        
         tstCtrl.image = 'test';
         tstCtrl.secondtext = 'test';
         tstCtrl.subject = 'test';
         tstCtrl.firsttext = 'test';
         tstCtrl.pageBreak = false;
         tstCtrl.pageBreak1 = false;
         tstCtrl.pageBreak2 = false;
         tstCtrl.pageBreak3 = false;
         tstCtrl.qDate = System.today();
     } 
        
        @testSetup static void Setup(){
            Id pricebookId = Test.getStandardPricebookId();
            
            Product2 pro = new Product2();
            pro.name = 'test';
            pro.Produktbeschreibung_detailliert__c = 'test';
            pro.Family = 'Anzeigen Flat'; 
            pro.IsActive = true;
            insert pro;
            
            Product2 pro1 = new Product2();
            pro1.name = 'test1';
            pro1.Produktbeschreibung_detailliert__c = 'test';
            pro1.Family = 'Exklusiv Logo'; 
            pro1.IsActive = true;
            insert pro1;
            
            Product2 pro2 = new Product2();
            pro2.name = 'test2';
            pro2.Produktbeschreibung_detailliert__c = 'test';
            pro2.Family = 'Top Job'; 
            pro2.IsActive = true;
            insert pro2;
            
            Product2 pro3 = new Product2();
            pro3.name = 'test3';
            pro3.Produktbeschreibung_detailliert__c = 'test';
            pro3.Family = 'Dienstleistungen'; 
            pro3.IsActive = true;
            insert pro3;
            
            Product2 pro4 = new Product2();
            pro4.name = 'test4';
            pro4.Produktbeschreibung_detailliert__c = 'test';
            pro4.Family = 'Sonstiges'; 
            pro4.IsActive = true;
            insert pro4;
                        
            PricebookEntry pEntry = new PricebookEntry (); 
            pEntry.Product2Id = pro.id;
            pEntry.UseStandardPrice = false;
            pEntry.UnitPrice = 600; 
            pEntry.Pricebook2Id = pricebookId;
            pEntry.IsActive = true;
            insert pEntry; 
            
            PricebookEntry pEntry1 = new PricebookEntry (); 
            pEntry1.Product2Id = pro1.id;
            pEntry1.UseStandardPrice = false;
            pEntry1.UnitPrice = 600; 
            pEntry1.Pricebook2Id = pricebookId;
            pEntry1.IsActive = true;
            insert pEntry1;
            
            PricebookEntry pEntry2 = new PricebookEntry (); 
            pEntry2.Product2Id = pro2.id;
            pEntry2.UseStandardPrice = false;
            pEntry2.UnitPrice = 600; 
            pEntry2.Pricebook2Id = pricebookId;
            pEntry2.IsActive = true;
            insert pEntry2;
            
            PricebookEntry pEntry3 = new PricebookEntry (); 
            pEntry3.Product2Id = pro3.id;
            pEntry3.UseStandardPrice = false;
            pEntry3.UnitPrice = 600; 
            pEntry3.Pricebook2Id = pricebookId;
            pEntry3.IsActive = true;
            insert pEntry3;
            
            PricebookEntry pEntry4 = new PricebookEntry (); 
            pEntry4.Product2Id = pro4.id;
            pEntry4.UseStandardPrice = false;
            pEntry4.UnitPrice = 600; 
            pEntry4.Pricebook2Id = pricebookId;
            pEntry4.IsActive = true;
            insert pEntry4;
        
            Account acc = new Account();
            acc.name = 'testAcc';
            acc.BillingStreet = 'testStreet';
            acc.BillingCity = 'München';
            acc.BillingPostalCode = '80331';
            acc.BillingCountry = 'Deutschland';
            acc.RecordTypeId = '012D0000000BZ8L';
            insert acc;
            
            Account acc1 = new Account();
            acc1.name = 'testAcc1';
            acc1.BillingStreet = 'testStreet';
            acc1.BillingCity = 'Brüssel';
            acc1.BillingPostalCode = '020203';
            acc1.BillingCountry = 'Belgien';
            acc1.ON_UID_Nummer__c = 'U2323232';
            acc1.RecordTypeId = '012D0000000BZ8L';
            insert acc1;
            
            // Formelfeld --> query
            Account tstAcc = [Select ON_Steuerart__c FROM Account WHERE name = 'testAcc1'];
            system.assertEquals('Nicht steuerbar (EU mit UStID-Nr.)', tstAcc.ON_Steuerart__c );

            
            Contact con = new Contact();
            con.AccountId = acc.id;
            con.FirstName = 'böser';
            con.LastName = 'Moritz';
            con.Salutation = 'Herr';
            con.Briefanrede__c = 'Sehr geehrter';
            con.Entscheider__c = true;
            insert con;
            
            Contact con1 = new Contact();
            con1.AccountId = acc1.id;
            con1.FirstName = 'netter';
            con1.LastName = 'Moritz';
            con1.Salutation = 'Herr';
            con1.Briefanrede__c = 'Sehr geehrter';
            con1.Entscheider__c = true;
            insert con1;
            
            
            Opportunity opp = new Opportunity ();
            opp.AccountId = acc.Id; 
            opp.Name = 'test';
            opp.StageName = '40 - Demo Termin vereinbart';
            opp.Override_Region__c = 'München';
            opp.CloseDate = System.today();
            opp.z_Entscheider__c = con.id;
            opp.z_Vertragsbeginn__c = system.today();
            opp.z_Enddatum__c = system.today().addDays(360);
            opp.Type = 'Bestandskundengeschäft'; 
            opp.ON_Kein_Rabatt_ausweisen__c = false; 
            insert opp;
            
            system.assertEquals('test', opp.Name);

            Opportunity opp1 = new Opportunity ();
            opp1.AccountId = acc.Id; 
            opp1.Name = 'test1';
            opp1.StageName = '40 - Demo Termin vereinbart';
            opp1.Override_Region__c = 'München';
            opp1.CloseDate = System.today();
            opp1.z_Entscheider__c = con.id;
            opp1.z_Vertragsbeginn__c = system.today();
            opp1.z_Enddatum__c = system.today().addDays(360);
            opp1.Type = 'Bestandskundengeschäft'; 
            opp1.ON_Kein_Rabatt_ausweisen__c = true; 
            insert opp1;
            
            Opportunity opp2 = new Opportunity ();
            opp2.AccountId = acc1.Id; 
            opp2.Name = 'test2';
            opp2.StageName = '40 - Demo Termin vereinbart';
            opp2.Override_Region__c = 'München';
            opp2.CloseDate = System.today();
            opp2.z_Entscheider__c = con1.id;
            opp2.z_Vertragsbeginn__c = system.today();
            opp2.z_Enddatum__c = system.today().addDays(360);
            opp2.Type = 'Bestandskundengeschäft'; 
            opp2.ON_Kein_Rabatt_ausweisen__c = true; 
            insert opp2;
            
            OpportunityLineItem oli = new OpportunityLineItem ();
            oli.TotalPrice = 500;
            oli.Produkt__c = 'test';
            oli.Quantity = 4;
            oli.OpportunityId = opp.Id;
            oli.Product2Id = pro.id;
            oli.ON_Produktbeschreibung_detailliert__c = 'test';                            
            oli.PricebookEntryId = pEntry.id;
            insert oli;
            
            OpportunityLineItem oli1 = new OpportunityLineItem ();
            oli1.TotalPrice = 600;
            oli1.Produkt__c = 'test1';
            oli1.Quantity = 4;
            oli1.OpportunityId = opp.Id;
            oli1.Product2Id = pro1.id;
            oli1.ON_Produktbeschreibung_detailliert__c = 'test';                            
            oli1.PricebookEntryId = pEntry1.id;
            insert oli1;
            
            OpportunityLineItem oli2 = new OpportunityLineItem ();
            oli2.TotalPrice = 1000;
            oli2.Produkt__c = 'test2'; 
            oli2.Quantity = 4;
            oli2.OpportunityId = opp.Id;
            oli2.Product2Id = pro2.id;
            oli2.ON_Produktbeschreibung_detailliert__c = 'test';                            
            oli2.PricebookEntryId = pEntry2.id;
            insert oli2;
            
            OpportunityLineItem oli3 = new OpportunityLineItem ();
            oli3.TotalPrice = 1000;
            oli3.Produkt__c = 'test2'; 
            oli3.Quantity = 4;
            oli3.OpportunityId = opp.Id;
            oli3.Product2Id = pro3.id;
            oli3.ON_Produktbeschreibung_detailliert__c = 'test';                            
            oli3.PricebookEntryId = pEntry3.id;
            insert oli3;
            
            OpportunityLineItem oli4 = new OpportunityLineItem ();
            oli4.TotalPrice = 1000;
            oli4.Produkt__c = 'test2'; 
            oli4.Quantity = 4;
            oli4.OpportunityId = opp.Id;
            oli4.Product2Id = pro4.id;
            oli4.ON_Produktbeschreibung_detailliert__c = 'test';                            
            oli4.PricebookEntryId = pEntry4.id;
            insert oli4;
    
        }
    
        // positiver Test
        @isTest static void testSectionGenerator(){

            Opportunity opp = [SELECT Id, Name FROM Opportunity WHERE Name = 'test' LIMIT 1]; 
            System.assertEquals(opp.name, 'test');
            List<OpportunityLineItem> testList = [Select Id, Product2.CustomQuoteCategory__c FROM OpportunityLineItem  WHERE OpportunityId =: opp.Id Limit 50];
            System.assertEquals(testList.isEmpty(), false);

            ApexPages.StandardController sc = new ApexPages.StandardController(opp);
            CreateCustomOpporutnityQuote tstCtrl = new CreateCustomOpporutnityQuote(sc);
            Test.startTest();
            tstCtrl.createPdfSections();
            List<OpportunityLineItem> bas = tstCtrl.sectionFlat;
            List<OpportunityLineItem> emp = tstCtrl.sectionEmployerBranding;
            List<OpportunityLineItem> perv = tstCtrl.sectionPerformance;
            List<OpportunityLineItem> serv = tstCtrl.sectionService;
            List<OpportunityLineItem> sons = tstCtrl.sectionSonstiges;
            Test.stopTest();
        }
}
What i am doing wrong ? The switch statment is not covered and if i call the methode, it does not create values for the list variables.  

 
Andrew GAndrew G
Hi

Your switch statement is using CustomQuoteCategory__c  on the Product record, which does not appear to be set in the Test data that you are inserting.  (unless this is set by some other automation on insert).

Regards
Andrew
 
Deepali KulshresthaDeepali Kulshrestha
Hi Marry,

The switch statement is working on the CustomQuoteCategory__c field of the product but you are not assigning anything when you are creating a product. So Include this CustomQuoteCategory__c  field in your product.

Like this:
 
            Product2 pro = new Product2();
            pro.name = 'test';
            pro.Produktbeschreibung_detailliert__c = 'test';
            pro.Family = 'Anzeigen Flat'; 
            pro.IsActive = true;
            pro.CustomQuoteCategory__c  = 'BAS';
            insert pro;

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too

Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com
Marry SteinMarry Stein
Hi Andrew, thanks for your response. CustomQuoteCategory__c is a formula field related to the product family field. I have checked the values and each of them is set correctly. 
Andrew GAndrew G
Apologies for the delayed response, but if you haven't resolved this one, could you try tweaking your code:
 
String switchTest;
      for(OpportunityLineItem oli : oliList){
        switchTest = oli.Product2.CustomQuoteCategory__c ;
        System.debug('&&&debug - :' + switchTest );
            switch on switchTest  {
                when 'BAS' {
                    sectionFlat.add(oli);
               }
                when 'EMP' {
                    sectionEmployerBranding.add(oli); 
                }
                when 'PER'{
                    sectionPerformance.add(oli); 
                }
                when 'SER'{
                    sectionService.add(oli); 
                }
                when else {
                    sectionSonstiges.add(oli);
                }
            }
        }
Lets just confirm that the value is populated and that it assignable.  If necessary, turn on the debug as you run your test code.

Regards
Andrew