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
Doug Traster 4Doug Traster 4 

Test case coverage on trigger for Mapping Strings and if, else

Hi All;

I have created a trigger that pulls Product names and puts them on the Opportunity so that I can see them in a related list on Account.  

I have a test case that gets me 82%, but want to push it higher in case data or something else changes.

Here is my trigger:
trigger ShowOppProd on Opportunity (before update,before insert) {
 Map<Id,Set<String>> prodToOpp = new Map<Id,Set<String>> ();
    for (Opportunity opp : [SELECT Id, Opportunity_Prod__c, (SELECT Id, OpportunityId, Product2.name
                                                           FROM OpportunityLineItems)FROM Opportunity 
                            WHERE Opportunity.Id IN: Trigger.new] ) {
         
         Set<String> productsInOpp = new Set<String>();
         for (OpportunityLineItem oli : opp.opportunityLineItems)
             productsInOpp.add(oli.Product2.name);
         
                                if(prodToOpp.containsKey(opp.Id)){
                                 Set<String> uniqueProdset = prodToOpp.get(opp.Id);
                                    uniqueProdset.addAll(productsInOpp);
                                    prodToOpp.put(opp.Id,uniqueProdset);
                                }
                                else
                                  prodToOpp.put(opp.Id,productsInOpp);
                                
                            }
    for (Opportunity o: Trigger.new){
        if(prodToOpp.containsKey(o.Id)){
            String concatProd = '';
            for (String p : prodToOpp.get(o.Id))
                concatProd = concatProd + (concatProd.length() == 0 ? '' : '; ')+ p;
            o.Opportunity_Prod__c = concatProd;
        }
    
}
}

Here is my test case:
@isTest
public class OppProdTest {
    static testMethod void TestOppProd() {
        
        Account a = new Account (
             Name = 'The High School',
             Organization_Type__c = 'High School'
        );
        
        Insert a;
        
        
        Product2 prod = new Product2(Name = 'Bronze',
                                     Family = 'test'
                                    );
        Insert prod;
        
        Id pricebookId = Test.getStandardPricebookId();
        
        PricebookEntry standardPrice = new PricebookEntry (
             Pricebook2Id = pricebookId, 
             Product2Id = prod.Id,
             UnitPrice = 500, 
             IsActive = true
        );
        
        Insert standardPrice;
        
        Pricebook2 customPB = new Pricebook2(
             Name = 'Custom Pricebook',
             isActive = true
        );
        
        Insert customPB;
        
        
        PriceBookEntry customPrice = new PricebookEntry(
             Pricebook2Id = customPB.Id,
             Product2Id = prod.Id,
             UnitPrice = 350,
             IsActive = true
    
        );
        
        Insert customPrice;
        
     
        Opportunity opp = new Opportunity(
             Account = a,
             Opportunity_Prod__c = prod.Name,
             Name = 'Test Opp',
             StageName = 'Proposal',
             CloseDate = System.today()
        );
        
        Insert opp;     
        
        
        OpportunityLineItem OLI = new OpportunityLineItem(
             PricebookEntryId = standardPrice.Id,
             OpportunityId = opp.Id,
             Quantity = 1,
             TotalPrice = standardPrice.UnitPrice 
         );
        
        Insert OLI;
               
        
       System.assertEquals('Bronze', opp.Opportunity_Prod__c);
                             
                
         }                                   
}

Running the issues I am getting that these lines are not under coverage. I am at a loss on how to add this to the test.  I know I probably need to add another assert somewhere, but cant figure it out.  

                          Set<String> uniqueProdset = prodToOpp.get(opp.Id);
                                    uniqueProdset.addAll(productsInOpp);
                                    prodToOpp.put(opp.Id,uniqueProdset);

Any help would be most appreciated!

Thanks!
LBKLBK
Hey Doug,

Try this test class.
@isTest
public class OppProdTest {
    static testMethod void TestOppProd() {
        
        Account a = new Account (
             Name = 'The High School',
             Organization_Type__c = 'High School'
        );
        
        Insert a;
        
        Product2 prod = new Product2(Name = 'Bronze',
                                     Family = 'test'
                                    );
        Insert prod;
        
        Id pricebookId = Test.getStandardPricebookId();
        
        PricebookEntry standardPrice = new PricebookEntry (
             Pricebook2Id = pricebookId, 
             Product2Id = prod.Id,
             UnitPrice = 500, 
             IsActive = true
        );      
        Insert standardPrice;
        
        Pricebook2 customPB = new Pricebook2(
             Name = 'Custom Pricebook',
             isActive = true
        );
        
        Insert customPB;
        
        PriceBookEntry customPrice = new PricebookEntry(
             Pricebook2Id = customPB.Id,
             Product2Id = prod.Id,
             UnitPrice = 350,
             IsActive = true
    
        );        
        Insert customPrice;
		
		//Product 2
		        Product2 prod2 = new Product2(Name = 'Silver',
                                     Family = 'test'
                                    );
        Insert prod2;
        
        Id pricebookId2 = Test.getStandardPricebookId();
        
        PricebookEntry standardPrice2 = new PricebookEntry (
             Pricebook2Id = pricebookId2, 
             Product2Id = prod2.Id,
             UnitPrice = 500, 
             IsActive = true
        );      
        Insert standardPrice2;
        
        Pricebook2 customPB2 = new Pricebook2(
             Name = 'Custom Pricebook2',
             isActive = true
        );
        
        Insert customPB2;
        
        PriceBookEntry customPrice2 = new PricebookEntry(
             Pricebook2Id = customPB2.Id,
             Product2Id = prod2.Id,
             UnitPrice = 350,
             IsActive = true
    
        );        
        Insert customPrice2;
		//Product 2
		
        Opportunity opp = new Opportunity(
             Account = a,
             Opportunity_Prod__c = prod.Name,
             Name = 'Test Opp',
             StageName = 'Proposal',
             CloseDate = System.today()
        );
        
        Insert opp;     
        
        OpportunityLineItem OLI = new OpportunityLineItem(
             PricebookEntryId = standardPrice.Id,
             OpportunityId = opp.Id,
             Quantity = 1,
             TotalPrice = standardPrice.UnitPrice 
         );        
        Insert OLI;
		
        OpportunityLineItem OLI2 = new OpportunityLineItem(
             PricebookEntryId = standardPrice2.Id,
             OpportunityId = opp.Id,
             Quantity = 1,
             TotalPrice = standardPrice2.UnitPrice 
         );        
        Insert OLI2;
		System.assertEquals('Bronze', opp.Opportunity_Prod__c);
     }                                   
}
Let me know how it goes.
Doug Traster 4Doug Traster 4
LBK

Thanks, but it doesn't change my coverage.  The 3 lines I mentioned above is still not covered.  I think I need to get another assert into the test in some way to get this to work.
Arvind KumarArvind Kumar

Hi,

Could you please explain use case of below lines?

 

if(prodToOpp.containsKey(opp.Id)){
                                 Set<String> uniqueProdset = prodToOpp.get(opp.Id);
                                    uniqueProdset.addAll(productsInOpp);
                                    prodToOpp.put(opp.Id,uniqueProdset);
                                }

Thanks in advance