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
LightsLights 

Test Class help for an Apex Trigger

I have the following apex trigger and I need a test class. Can you guys check what I got so far?

Apex Trigger
trigger NOVEMBER2 on OpportunityLineItem( after insert, after update )
{
    List<OpportunityLineItem> oliList = new List<OpportunityLineItem>();
    Set<Id> opptyIds = new Set<Id>();
 
    for( OpportunityLineItem optLineItem: trigger.new )
    {
        if( optLineItem.ProductCode == '00002a' )
        {
            opptyIds.add( optLineItem.OpportunityId );
        }
    }
 
    if( opptyIds.size() > 0 )
    {
        //retrieve the values based on Product list
        List<OpportunityLineItem> lstOpptyLineItems = [ SELECT Opportunity.Pricebook2Id, OpportunityId, Name, ProductCode,
                                                        PricebookEntryId, Quantity, UnitPrice
                                                        FROM OpportunityLineItem
                                                        WHERE OpportunityId IN: opptyIds order by ProductCode ];
 
        Map<Id, List<OpportunityLineItem>> mapOpptyLineItem = new Map<Id, List<OpportunityLineItem>>();
        for( OpportunityLineItem item : lstOpptyLineItems )
        {
            List<OpportunityLineItem> oliList1 = new List<OpportunityLineItem>();
            if( mapOpptyLineItem.containsKey( item.OpportunityId ))
            {
                oliList1 = mapOpptyLineItem.get( item.OpportunityId );
            }
            
            oliList1.add( item );
            mapOpptyLineItem.put( item.OpportunityId, oliList1 );
        }
 
 
        //retrieve PriceBookEntry of the Product B, this is most important
        PricebookEntry pbeProduct2 = [ SELECT Id, Pricebook2Id, UnitPrice, Name, Pass_Through_Percentage_Entry__c
                                        FROM PricebookEntry
                                        WHERE Name ='Car Oil Fee'
                                        AND Pricebook2Id  IN (SELECT Id FROM PriceBook2 WHERE Id ='01sA00000004lbRIAQ')
                                        LIMIT 1 ];
 
         if( trigger.isInsert )
         {
             for( Id oppId : mapOpptyLineItem.keySet() )
             {
                 for( OpportunityLineItem item : mapOpptyLineItem.get( oppId ))
                 {
                     if( item.ProductCode == '00002a' )
                     {
                         oliList.add( new OpportunityLineItem(
                                            OpportunityId = oppId,
                                            PricebookEntryId = pbeProduct2.Id,
                                            Quantity = item.Quantity,
                                            UnitPrice = item.UnitPrice * pbeProduct2.Pass_Through_Percentage_Entry__c * 0.01 )
                                          );
                     }
                 }
             }
             
             if( oliList.size() > 0 )
                 insert oliList;
         }
         else if( trigger.isUpdate )
         {
             for( Id oppId : mapOpptyLineItem.keySet() )
             {
                 OpportunityLineItem itemProductA = new OpportunityLineItem();
                 for( OpportunityLineItem item : mapOpptyLineItem.get( oppId ))
                 {
                     if( item.ProductCode == '00002a' )
                     {
                         itemProductA = item;
                         continue;
                     }
                     if( item.ProductCode == '00002b' )
                     {
                         oliList.add( new OpportunityLineItem( Id = item.Id,
                                            OpportunityId = oppId,
                                            PricebookEntryId = pbeProduct2.Id,
                                            Quantity = itemProductA.Quantity,
                                            UnitPrice = itemProductA.UnitPrice * pbeProduct2.Pass_Through_Percentage_Entry__c * 0.01 )
                                          );
                     }
                 }
             }
             
             if( oliList.size() > 0 )
                 update oliList;
         }
    }
}
Test Class:
@isTest
private class OpportunityLineItemTrigger_Test{
  static testMethod void test_OpportunityLineItemTrigger(){
   test.startTest();
    OpportunityLineItem opportunitylineitem_Obj = new OpportunityLineItem(OpportunityId = opportunity_Obj[0].id, PricebookEntryId = pricebookentry_Obj[0].id, Quantity = 9, UnitPrice = 13, Package_Delivery_Date__c = false, Batch_Readiness__c = false);
    Insert opportunitylineitem_Obj; 
   test.stopTest();
  }

  static testMethod void test_UseCase2(){
   test.startTest();
    OpportunityLineItem opportunitylineitem_Obj = new OpportunityLineItem(OpportunityId = opportunity_Obj[0].id, PricebookEntryId = pricebookentry_Obj[0].id, Quantity = 9, UnitPrice = 13, Package_Delivery_Date__c = false, Batch_Readiness__c = false);
    opportunitylineitem_Obj.ProductCode='UseCase1-0';
    Insert opportunitylineitem_Obj; 
    test.stopTest();
}
}


 
Best Answer chosen by Lights
Neetu_BansalNeetu_Bansal
Hello,

Please try below test class:
@isTest( seeAllData = true )
private class OpportunityLineItemTrigger_Test
{
	static testMethod void test_OpportunityLineItemTrigger()
	{
		Account acc = new Account( Name = 'Test' );
		insert acc;
		
		Opportunity opp = new Opportunity( Name = 'Test', AccountId = acc.Id, StageName = 'New', CloseDate = Date.today());
		insert opp;
		
		Pricebook2 standard = [Select Id, Name, IsActive From Pricebook2 where Id = '01sA00000004lbRIAQ' ];
		
		Product2 prod =new Product2( Name='test', ProductCode='00002a', isActive = true );
        insert prod;
		
		PricebookEntry pbe = new PricebookEntry( pricebook2Id = standard.id, product2id = prod.id, unitprice = 1249.0, isactive = true,
													Name = 'Car Oil Fee' );
	    insert pbe;
		
		OpportunityLineItem opportunitylineitem_Obj = new OpportunityLineItem( OpportunityId = opp.id, PricebookEntryId = pbe.id, Quantity = 9,
																				UnitPrice = 13, Package_Delivery_Date__c = false,
																				Batch_Readiness__c = false );
		test.startTest();
			insert opportunitylineitem_Obj;
		test.stopTest();
	}

	static testMethod void test_UseCase2()
	{
		Account acc = new Account( Name = 'Test' );
		insert acc;
		
		Opportunity opp = new Opportunity( Name = 'Test', AccountId = acc.Id, StageName = 'New', CloseDate = Date.today() );
		insert opp;
		
		Pricebook2 standard = [Select Id, Name, IsActive From Pricebook2 where Id = '01sA00000004lbRIAQ' ];
		
		Product2 prod =new Product2( Name='test', ProductCode='00002a', isActive = true );
        insert prod;
		
		PricebookEntry pbe = new PricebookEntry( pricebook2Id = standard.id, product2id = prod.id, unitprice = 1249.0, isactive = true,
													Name = 'Car Oil Fee' );
	    insert pbe;
		
		OpportunityLineItem opportunitylineitem_Obj = new OpportunityLineItem( OpportunityId = opp.id, PricebookEntryId = pbe.id, Quantity = 9,
																				UnitPrice = 13, Package_Delivery_Date__c = false,
																				Batch_Readiness__c = false );
		test.startTest();
			insert opportunitylineitem_Obj;
			
			opportunitylineitem_Obj.Quantity = 10;
			update opportunitylineitem_Obj;
		test.stopTest();
	}
}
Also, I suggest not to hard code Id either in Trigger and test class, as it will change in any other Salesforce Environment.

​Thanks,
Neetu

All Answers

Neetu_BansalNeetu_Bansal
Hello,

Please try below test class:
@isTest( seeAllData = true )
private class OpportunityLineItemTrigger_Test
{
	static testMethod void test_OpportunityLineItemTrigger()
	{
		Account acc = new Account( Name = 'Test' );
		insert acc;
		
		Opportunity opp = new Opportunity( Name = 'Test', AccountId = acc.Id, StageName = 'New', CloseDate = Date.today());
		insert opp;
		
		Pricebook2 standard = [Select Id, Name, IsActive From Pricebook2 where Id = '01sA00000004lbRIAQ' ];
		
		Product2 prod =new Product2( Name='test', ProductCode='00002a', isActive = true );
        insert prod;
		
		PricebookEntry pbe = new PricebookEntry( pricebook2Id = standard.id, product2id = prod.id, unitprice = 1249.0, isactive = true,
													Name = 'Car Oil Fee' );
	    insert pbe;
		
		OpportunityLineItem opportunitylineitem_Obj = new OpportunityLineItem( OpportunityId = opp.id, PricebookEntryId = pbe.id, Quantity = 9,
																				UnitPrice = 13, Package_Delivery_Date__c = false,
																				Batch_Readiness__c = false );
		test.startTest();
			insert opportunitylineitem_Obj;
		test.stopTest();
	}

	static testMethod void test_UseCase2()
	{
		Account acc = new Account( Name = 'Test' );
		insert acc;
		
		Opportunity opp = new Opportunity( Name = 'Test', AccountId = acc.Id, StageName = 'New', CloseDate = Date.today() );
		insert opp;
		
		Pricebook2 standard = [Select Id, Name, IsActive From Pricebook2 where Id = '01sA00000004lbRIAQ' ];
		
		Product2 prod =new Product2( Name='test', ProductCode='00002a', isActive = true );
        insert prod;
		
		PricebookEntry pbe = new PricebookEntry( pricebook2Id = standard.id, product2id = prod.id, unitprice = 1249.0, isactive = true,
													Name = 'Car Oil Fee' );
	    insert pbe;
		
		OpportunityLineItem opportunitylineitem_Obj = new OpportunityLineItem( OpportunityId = opp.id, PricebookEntryId = pbe.id, Quantity = 9,
																				UnitPrice = 13, Package_Delivery_Date__c = false,
																				Batch_Readiness__c = false );
		test.startTest();
			insert opportunitylineitem_Obj;
			
			opportunitylineitem_Obj.Quantity = 10;
			update opportunitylineitem_Obj;
		test.stopTest();
	}
}
Also, I suggest not to hard code Id either in Trigger and test class, as it will change in any other Salesforce Environment.

​Thanks,
Neetu
This was selected as the best answer
Kartheek VarmaKartheek Varma

How to erite the test class in bel;ow trigger?
trigger SendAdmitCard on Admit_Card__c (after update) {
    
    Set<Admit_Card__c> admitCardDetails = new Set<Admit_Card__c>();
    if(Trigger.isInsert || Trigger.isUpdate) {
    for(Admit_Card__c admitCard : trigger.new){
        if(admitCard.FUA_Broadcast_Admit_Card__c){
            admitCardDetails.add(admitCard);
        }
    }
    }
    if(admitCardDetails.size()>0) AdmitCardDetailsHandler.CreateAdmitCard(admitCardDetails);
   
  
  
   
}