+ Start a Discussion
Sreekaanth reddySreekaanth reddy 

How to write test class for below apex class

public class ProductLighting {
 @AuraEnabled
    public static Opportunity fetchOpportunityLineItem(String opId) {
        Opportunity returnContactList = new Opportunity();
        
        returnContactList = [Select Id,StageName,(SELECT Id,UnitPrice,Quantity,Discount__c, From OpportunityLineItems Where Approved_c=True) from Opportunity Where Id=:opId];
        
        return returnContactList ;
    }
    
    
    @AuraEnabled
    public static List < String > Records(List < String > lstRecordId) {
        List < String > Msg = new List < String > ();
        List < OpportunityLineItem > lstDeprecateRec = [select Id,Product_Deprecated__c from OpportunityLineItem where id IN: lstRecordId];
        for(OpportunityLineItem ord:lstDeprecateRec)
        {
            ord.Product_Deprecated__c=True;
        }
        allowupdateopplineitem__c obj = allowupdateopplineitem__c.getInstance('isallowed');
        
        obj.allowupdate__c = true;
        update obj;
        Database.SaveResult[] DR_Dels = Database.update(lstDeprecateRec, false);
         obj.allowupdate__c = false;
        update obj;
        for (Database.SaveResult dr: DR_Dels) {
            if (dr.isSuccess()) {
                system.debug('successful Deprecate Opportunity Line Items');
            } 
            }
        return Msg;
        
    }
}
 
CharuDuttCharuDutt
Hii Shreekanth
Try Below Test Code
@isTest
Public Class fetchOpportunityLineItemTest{
@isTest
Public Static void UnitTest(){
	Product2 p = new Product2();
p.Name = ' Test Product ';
p.Description='Test Product';
p.productCode = '123';
p.isActive = true;
insert p;

PricebookEntry standardPrice = new PricebookEntry();
standardPrice.Pricebook2Id = Test.getStandardPricebookId();
standardPrice.Product2Id = p.Id;
standardPrice.UnitPrice = 100;
standardPrice.IsActive = true;
standardPrice.UseStandardPrice = false;
insert standardPrice ;


Account acc = new Account(
Name = 'Test Account',
);
insert acc;


Opportunity opp = new Opportunity();
opp.AccountId = acc.Id;
opp.Name = 'Test Opportunity';
opp.CloseDate= System.Today();
opp.StageName='Prospecting';
opp.Pricebook2Id = Test.getStandardPricebookId();
insert opp;

OpportunityLineItem oppLineItem = new OpportunityLineItem();
oppLineItem.OpportunityId = opp.Id;
oppLineItem.PricebookEntryId = standardPrice.Id;
oppLineItem.UnitPrice = 7000;
oppLineItem.Quantity = 5;
oppLineItem.Discount__c = */Fill This/*;
oppLineItem.Approved_c = True;
oppLineItem.Product_Deprecated__c = False;
insert oppLineItem;

list<String> lstId = new list<String>();
        lstId.Add(oppLineItem.Id);

allowupdateopplineitem__c  obj = new allowupdateopplineitem__c ();
/*Fill Required Fields*/

ProductLighting.fetchOpportunityLineItem(opp.Id);
ProductLighting.Records(lstId);

	}
}
Please Mark It As Best Answer If It Helps
Thank You!

 
Suraj Tripathi 47Suraj Tripathi 47

Hi,

Please find the solution.

@isTest
public class ProductLightingTest{

    @isTest
    public static void data()
    {
	
	
// insert product
Product2 p = new Product2();
p.Name = ' Test Product ';
p.Description='Test Product Entry For Product';
p.productCode = 'SFDCPanther-123';
p.isActive = true;
insert p;

// insert pricebook entry for the product
PricebookEntry standardPrice = new PricebookEntry();
standardPrice.Pricebook2Id = Test.getStandardPricebookId();
standardPrice.Product2Id = p.Id;
standardPrice.UnitPrice = 100;
standardPrice.IsActive = true;
standardPrice.UseStandardPrice = false;
insert standardPrice ;

// insert account
Account acc = new Account(
Name = 'SFDCPanther.com',
Rating = 'Hot',
Industry = 'Banking',
Phone = '9087654321'
);
insert acc;

// Create Opportunity
Opportunity opp = new Opportunity();
opp.AccountId = acc.Id;
opp.Name = 'Test Opportunity';
opp.CloseDate= System.Today();
opp.StageName='Prospecting';
opp.Pricebook2Id = Test.getStandardPricebookId();
opp.Approved_c=true;
insert opp;

// Add product and Pricebook to the particular opportunity using OpportunityLineItem 
OpportunityLineItem oppLineItem = new OpportunityLineItem();
oppLineItem.OpportunityId = opp.Id;
oppLineItem.PricebookEntryId = standardPrice.Id;
oppLineItem.UnitPrice = 7000;
oppLineItem.Quantity = 5;
oppLineItem.Product_Deprecated__c=true;
insert oppLineItem;
	
	List<String> lstRecordId=new List<String>();
	lstRecordId.add(oppLineItem.id);
	
	 Test.startTest();
                     ProductLighting.fetchOpportunityLineItem(opp.id); 
                     ProductLighting.fetchOpportunityLineItem(lstRecordId); 					 
     Test.stopTest();
	
	
	}
	
	}


Please mark it as the Best Answer if it helps you

Thank You