+ Start a Discussion
Waqas AliWaqas Ali 

Unit test for opportunity Line Items

Here is my apex class copying the opportunity line items of current opportunity to newly created order and opportunity. I do not know how to write unit test for opportunity line items,
String theId = ApexPages.currentPage().getParameters().get('id');
 List<OpportunityLineItem> currentOppLI = [
                                SELECT PricebookEntryId, Quantity, UnitPrice,
                                       TotalPrice, ListPrice, Description
                                FROM OpportunityLineItem
                                WHERE OpportunityId = :theId];
 //Create the Order
                    Order odr = new Order(  
                    OpportunityId=o.id
                    ,AccountId = o.AccountId
                    ,Name = o.Name
                    ,EffectiveDate=Date.today()
                    ,Status='Draft'
                    ,Pricebook2Id = o.Pricebook2Id
                    );
                insert odr;
                //copy Opportunity line items to Order line items
                  List<OrderItem> newOrderProductsList = new List<OrderItem>();
    
        for (OpportunityLineItem item : currentOppLI) {
             newOrderProductsList.add(new OrderItem(
                    PricebookEntryId = item.PricebookEntryId,
                    Quantity = item.Quantity,
                    UnitPrice = item.UnitPrice,
                    Description = item.Description,
                    OrderId = odr.Id));
         }
             insert newOrderProductsList;  

 List<OpportunityLineItem> newOppLI = new List<OpportunityLineItem>();
        
        for (OpportunityLineItem item : currentOppLI) {
             newOppLI.add(new OpportunityLineItem(
             PricebookEntryId = item.PricebookEntryId,   
             Quantity = item.Quantity,
             UnitPrice = item.UnitPrice,
             Description = item.Description,
             OpportunityId = opp.Id));
         }
            
    insert newOppLI;   


//opp is newly created opportunity and odr is newly created order
How to write unit test for opportunity line items and order items??
 
Best Answer chosen by Waqas Ali
Amit Chaudhary 8Amit Chaudhary 8
Hi ,

Please check below blog for more information on test classess
http://amitsalesforce.blogspot.in/2015/06/best-practice-for-test-classes-sample.html

Please try below code. 
@isTest
private class TestUpdateClosedListPrice 
{
	static testMethod void TestUpdateClosedListPrice() 
	{
		Account acc = new Account(Name = 'Test Account');
		insert acc;
		
		//get standard pricebook
		// This is how we get the Standard PriceBook Id.  Prior to Summer '14, we needed
		// to use SeeAllData=true, so this is a big improvement
		Id pricebookId = Test.getStandardPricebookId();

		Product2 prd1 = new Product2 (Name='Test Product Entry 1',Description='Test Product Entry 1',productCode = 'ABC', isActive = true);
		insert prd1;
		
		PricebookEntry pbe1 = new PricebookEntry (Product2ID=prd1.id,Pricebook2ID=pricebookId,UnitPrice=50, isActive=true);
		insert pbe1;
		
		Opportunity opp1 = new Opportunity (Name='Opp1',StageName='Stage 0 - Lead Handed Off',CloseDate=Date.today(),Pricebook2Id = pbe1.Pricebook2Id, AccountId = acc.id);
		insert opp1;

		OpportunityLineItem lineItem1 = new OpportunityLineItem (OpportunityID=opp1.id,PriceBookEntryID=pbe1.id, quantity=4, totalprice=200);
		insert lineItem1;
		
		Test.startTest();
		
			// Please try below code if you are using StandardController
			PageReference pageRef = Page.AccountPlan; // Add your VF page Name here
			pageRef.getParameters().put('id', String.valueOf(opp1.Id));
			Test.setCurrentPage(pageRef);

			ApexPages.StandardController sc = new ApexPages.StandardController(opp1);
			myControllerExtension testAccPlan = new myControllerExtension(sc); // Add ur Controller Name
			

		Test.stopTest();
	}
}

Please modify the code in Test.startTest(); and Test.stopTest(); according to your apex class. I added sample code for Controller and StandardController

Test Class for Controller class
@isTest 
public class ControllerTestClass 
{
 static testMethod void testMethod1() 
 {
 Account testAccount = new Account();
 testAccount.Name='Test Account' ;
 insert testAccount;

 Test.StartTest(); 

  PageReference pageRef = Page.AccountPlan; // Add your VF page Name here
  pageRef.getParameters().put('id', String.valueOf(testAccount.Id));
  Test.setCurrentPage(pageRef);

  myController testAccPlan = new myController();
  
  //testAccPlan.save(); call all your function here
 Test.StopTest();
 }
}

Test Class for Standard Controller
@isTest 
public class ExtensionTestClass 
{
 static testMethod void testMethod1() 
 {
 Account testAccount = new Account();
 testAccount.Name='Test Account' ;
 insert testAccount;

 Test.StartTest(); 
  ApexPages.StandardController sc = new ApexPages.StandardController(testAccount);
  myControllerExtension testAccPlan = new myControllerExtension(sc);

  PageReference pageRef = Page.AccountPlan; // Add your VF page Name here
  pageRef.getParameters().put('id', String.valueOf(testAccount.Id));
  Test.setCurrentPage(pageRef);

  //testAccPlan.save(); call all your function here
 Test.StopTest();
 }
}

Please let us know if this will help u

Thanks,
Amit Chaudhary

All Answers

Amit Chaudhary 8Amit Chaudhary 8
Hi ,

Please check below blog for more information on test classess
http://amitsalesforce.blogspot.in/2015/06/best-practice-for-test-classes-sample.html

Please try below code. 
@isTest
private class TestUpdateClosedListPrice 
{
	static testMethod void TestUpdateClosedListPrice() 
	{
		Account acc = new Account(Name = 'Test Account');
		insert acc;
		
		//get standard pricebook
		// This is how we get the Standard PriceBook Id.  Prior to Summer '14, we needed
		// to use SeeAllData=true, so this is a big improvement
		Id pricebookId = Test.getStandardPricebookId();

		Product2 prd1 = new Product2 (Name='Test Product Entry 1',Description='Test Product Entry 1',productCode = 'ABC', isActive = true);
		insert prd1;
		
		PricebookEntry pbe1 = new PricebookEntry (Product2ID=prd1.id,Pricebook2ID=pricebookId,UnitPrice=50, isActive=true);
		insert pbe1;
		
		Opportunity opp1 = new Opportunity (Name='Opp1',StageName='Stage 0 - Lead Handed Off',CloseDate=Date.today(),Pricebook2Id = pbe1.Pricebook2Id, AccountId = acc.id);
		insert opp1;

		OpportunityLineItem lineItem1 = new OpportunityLineItem (OpportunityID=opp1.id,PriceBookEntryID=pbe1.id, quantity=4, totalprice=200);
		insert lineItem1;
		
		Test.startTest();
		
			// Please try below code if you are using StandardController
			PageReference pageRef = Page.AccountPlan; // Add your VF page Name here
			pageRef.getParameters().put('id', String.valueOf(opp1.Id));
			Test.setCurrentPage(pageRef);

			ApexPages.StandardController sc = new ApexPages.StandardController(opp1);
			myControllerExtension testAccPlan = new myControllerExtension(sc); // Add ur Controller Name
			

		Test.stopTest();
	}
}

Please modify the code in Test.startTest(); and Test.stopTest(); according to your apex class. I added sample code for Controller and StandardController

Test Class for Controller class
@isTest 
public class ControllerTestClass 
{
 static testMethod void testMethod1() 
 {
 Account testAccount = new Account();
 testAccount.Name='Test Account' ;
 insert testAccount;

 Test.StartTest(); 

  PageReference pageRef = Page.AccountPlan; // Add your VF page Name here
  pageRef.getParameters().put('id', String.valueOf(testAccount.Id));
  Test.setCurrentPage(pageRef);

  myController testAccPlan = new myController();
  
  //testAccPlan.save(); call all your function here
 Test.StopTest();
 }
}

Test Class for Standard Controller
@isTest 
public class ExtensionTestClass 
{
 static testMethod void testMethod1() 
 {
 Account testAccount = new Account();
 testAccount.Name='Test Account' ;
 insert testAccount;

 Test.StartTest(); 
  ApexPages.StandardController sc = new ApexPages.StandardController(testAccount);
  myControllerExtension testAccPlan = new myControllerExtension(sc);

  PageReference pageRef = Page.AccountPlan; // Add your VF page Name here
  pageRef.getParameters().put('id', String.valueOf(testAccount.Id));
  Test.setCurrentPage(pageRef);

  //testAccPlan.save(); call all your function here
 Test.StopTest();
 }
}

Please let us know if this will help u

Thanks,
Amit Chaudhary
This was selected as the best answer
Waqas AliWaqas Ali

Amit Chaudhary you are great, you are life saviour man. 

Thank You 

-waqas