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
sumit dsumit d 

test class for error message

Hi All,
       I have a trigger helper. I have made test class for it but its not covering the addErrorMethod().
       Can any one help me with this test class? and how can i insert assertequals method in it?
       trigger helper and test class is given below:-
public  without sharing class OpportunityLineItemTriggerHelper {
    
    public static List<OpportunityLineItem> newOpportunityLineItem = new List<OpportunityLineItem>();
    public static List<OpportunityLineItem> oldOpportunityLineItem = new List<OpportunityLineItem>();
    public static Map<Id, OpportunityLineItem> newMapOpportunityLineItem = new Map<Id, OpportunityLineItem>();
    public static Map<Id, OpportunityLineItem> oldMapOpportunityLineItem = new Map<Id, OpportunityLineItem>(); 
     public static void validateServiceAgreementProduct(){
        
        Map<Id, Product2> mapProductIdToProduct = new Map<Id, Product2>(
                                                                        [ SELECT Id, Name, Family
                                                                           FROM Product2 
                                                                           WHERE Family LIKE 'SA - %'
                                                                           OR Family LIKE 'Service - %'
                                                                        ]
                                                                        );
        Set<Id> parentOppIds = new Set<Id>();
        Set<Id> serviceProductIds = new Set<Id>();
        List<OpportunityLineItem> serviceProductLines = new List<OpportunityLineItem>();
        for( OpportunityLineItem oppLine : newOpportunityLineItem ){
            if( mapProductIdToProduct.containsKey( oppLine.Product2Id ) ){
                parentOppIds.add( oppLine.opportunityId );
                serviceProductLines.add( oppLine );
                serviceProductIds.add( oppLine.Product2Id );
            }
        }
        
        Map<Id, Opportunity> oppMap = new Map<Id, Opportunity>( [ SELECT Id, Name, 
                                                                 ( SELECT Id, Name, Product2Id 
                                                                   FROM OpportunityLineItems 
                                                                   Where Product2Id not in :serviceProductIds
                                                                 )
                                                                 FROM Opportunity
                                                                 WHERE Id IN: parentOppIds ] 
                                                               );
                                                               
        Map<Id, Set<Id>> mapOppIdToHardwareProductIds = new Map<Id, Set<Id>>();
        for( Opportunity opp : oppMap.values() ) {
            Set<Id> setHarwareProductIds = new Set<Id>();
            for( OpportunityLineItem opli : opp.OpportunityLineItems ) {
                setHarwareProductIds.add( opli.Product2Id );
            }
            mapOppIdToHardwareProductIds.put( opp.Id, setHarwareProductIds );
        }
        
        Map<Id, Set<Id>> mapServicesProductIDToHardwareProductIds = new Map<Id, Set<Id>>();
        for( SA_Junction__c sJunction : [ SELECT Id, Hardware_Product__c, 
                                          Services_Product__c 
                                          FROM SA_Junction__c 
                                          Where Services_Product__c in :serviceProductIds
                                         ] 
                                         ){
            Set<Id> hardwareProductIds = mapServicesProductIDToHardwareProductIds.get( sJunction.Services_Product__c );
            hardwareProductIds = hardwareProductIds == null ? new Set<Id>() : hardwareProductIds;
            hardwareProductIds.add( sJunction.Hardware_Product__c );                             
            mapServicesProductIDToHardwareProductIds.put( sJunction.Services_Product__c, hardwareProductIds );                                        
        }
        
        for( OpportunityLineItem oppLine : serviceProductLines ){
            Set<Id> actualHardwareProductIdsOnOpp = mapOppIdToHardwareProductIds.get( oppLine.OpportunityId );
            Set<Id> expectedHardwareProductIds = mapServicesProductIDToHardwareProductIds.get( oppLine.Product2Id );
            if( expectedHardwareProductIds != null && expectedHardwareProductIds.size() > 0 
                && actualHardwareProductIdsOnOpp != null && actualHardwareProductIdsOnOpp.size() > 0 ) {
                Boolean hasExpectedHardware = false;
                for( Id hardwareProdId : actualHardwareProductIdsOnOpp ) {
                    if( expectedHardwareProductIds.contains( hardwareProdId )) {
                        hasExpectedHardware = true;
                        break;  
                    }
                }
                if( !hasExpectedHardware ) {  
                    oppLine.addError('You need to select the correct agreement based on hardware products.'); 
                }
            }
        }
    }
}
test class for helper:-

 public static void validateServiceAgreementProductTest(){
        Account accTest = new Account( Name = 'Test Account' );
        insert accTest;
        
        Opportunity oppTest = new Opportunity( Name = 'Test Opportunity',
                                              StageName = 'Working',
                                              CloseDate = System.today(),
                                              AccountId = accTest.Id );
        insert oppTest;
        
        Product2 prod = new Product2( Name ='EV - PM1H0000BD',
                                     isActive = TRUE,
                                     Family = ' Printers' );
        insert prod;
        
        Product2 prod1 = new Product2( Name ='Service Agreement - Full - Primacy Dual',
                                      isActive = TRUE,
                                      Family = ' SA - Hardware' );
        insert prod1;
        
        OpportunityLineItem oppLine = new OpportunityLineItem( OpportunityId = oppTest.Id,
                                                              Quantity = 2,
                                                              UnitPrice = 10,
                                                              Product2Id = prod.id
                                                             );
        insert oppLine;
        
        OpportunityLineItem oppLine1 = new OpportunityLineItem( OpportunityId = oppTest.Id,
                                                               Quantity = 2,
                                                               UnitPrice = 10,
                                                               Product2Id = prod1.id
                                                              );
        insert oppLine1;
        
        //System.assertEquals(, );
    }
    
    public static void validateServiceAgreementProductTestError(){
        Account accTest = new Account( Name = 'Test Account' );
        insert accTest;
        
        Opportunity oppTest = new Opportunity( Name = 'Test Opportunity',
                                              StageName = 'Working',
                                              CloseDate = System.today(),
                                              AccountId = accTest.Id );
        insert oppTest;
        
        Product2 prod = new Product2( Name ='EV - PM1H0000BD',
                                     isActive = TRUE,
                                     Family = ' Printers' );
        insert prod;
        
        Product2 prod1 = new Product2( Name ='Service Agreement - Full - SD260',
                                      isActive = TRUE,
                                      Family = ' SA - Hardware' );
        insert prod1;
        
        OpportunityLineItem oppLine = new OpportunityLineItem( OpportunityId = oppTest.Id,
                                                              Quantity = 2,
                                                              UnitPrice = 10,
                                                              Product2Id = prod.id
                                                             );
        insert oppLine;
        
        OpportunityLineItem oppLine1 = new OpportunityLineItem( OpportunityId = oppTest.Id,
                                                               Quantity = 2,
                                                               UnitPrice = 10,
                                                               Product2Id = prod1.id
                                                              );
        try{
             insert oppLine1;
        }
       catch(Exception e) {
            System.assert(e.getMessage().contains('You need to select the correct agreement based on hardware products.'));
        }
         
    }
}