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
Mikasa AckermanMikasa Ackerman 

How write test class for the following code it:-

How write test class for the following code it:-
    
        public static void script(){
            List<OpportunityLineItem> oppLine= new List<OpportunityLineItem>();
    oppLine=[Select TotalPrice from OpportunityLineItem where Name like '%abc%' AND OpportunityId IN (Select id from Opportunity Where StageName='Closed Won')];
     System.debug(oppLine);

     Decimal tp=0;

    for(OpportunityLineItem op1:oppLine){
        tp=tp+op1.TotalPrice;
    }
    System.debug(tp);
        }
    
    I could not reach the 75% for above code.
    Thanks
Best Answer chosen by Mikasa Ackerman
Akshay Dhiman 63Akshay Dhiman 63
Hi Mikasa,

Below test class works fine please this out:-

@isTest
private class ScriptTest {

    @isTest static void scriptt(){
               Account acc = new Account(Name = 'Test');
        insert acc;
        
        Opportunity opp = new Opportunity(Name = 'Test Opp',AccountID = acc.Id,Amount = 100,CloseDate=Date.today(),StageName='Closed Won');
        insert opp;
        
        Id pricebookId = Test.getStandardPricebookId();
        
        Product2 prod = new Product2(
            Name = 'abc',
            ProductCode = '123',
            isActive = true
        );
        insert prod;
        
        PricebookEntry pbEntry = new PricebookEntry(
            Pricebook2Id = pricebookId,
            Product2Id = prod.Id,
            UnitPrice = 10.00,
            IsActive = true
        );
        insert pbEntry;
        
        OpportunityLineItem oli = new OpportunityLineItem(
            OpportunityId = opp.Id,
            Quantity = 5,
            PricebookEntryId = pbEntry.Id,
            TotalPrice = 5 * pbEntry.UnitPrice
        );
        insert oli;
                
        Test.startTest();
            Class_Name.script(); 
        Test.stopTest();
    }
        }
    }
    
Hope this explanation will resolve your query. Mark it as the best answer if you find it helpful.
Thanks
Akshay

All Answers

Maharajan CMaharajan C
Hi Mikasa,

Please try the below test class:

Change this line No 39  scriptController.script();  --> use your class Name . Method Name

Add the mandatory fields if any missing for below Account, Opportunity creations below.
@isTest
public class scriptControllerTest {
    @isTest static void scriptTest(){
        Account acc = new Account(Name = 'Test');
        insert acc;
        
        Opportunity opp = new Opportunity(Name = 'Test Opp',AccountID = acc.Id,Amount = 2000,CloseDate=Date.today(),StageName='Closed Won',Type='New Customer');
        insert opp;
        
        Id pricebookId = Test.getStandardPricebookId();
        
        //Create your product
        Product2 prod = new Product2(
            Name = 'abc',
            ProductCode = '123',
            isActive = true
        );
        insert prod;
        
        //Create your pricebook entry
        PricebookEntry pbEntry = new PricebookEntry(
            Pricebook2Id = pricebookId,
            Product2Id = prod.Id,
            UnitPrice = 100.00,
            IsActive = true
        );
        insert pbEntry;
        
        //create your opportunity line item.  This assumes you already have an opportunity created, called opp
        OpportunityLineItem oli = new OpportunityLineItem(
            OpportunityId = opp.Id,
            Quantity = 5,
            PricebookEntryId = pbEntry.Id,
            TotalPrice = 5 * pbEntry.UnitPrice
        );
        insert oli;
                
        Test.startTest();
        	scriptController.script();  // Add your class name here 
        Test.stopTest();
    }
}


Thanks,
Maharajan.C
Malika Pathak 9Malika Pathak 9

Hi Mikasa Ackerman,

Please Find the solution of your question How write test class for the following code it:-

@isTest
public class YA3quest_Test {
    @isTest
    public static void method1(){
        
        Opportunity op = new Opportunity();
        op.Name='Sales';
        op.StageName='Closed Won';
        op.CloseDate=Date.today();
        insert op;
        
        Product2 prdct = new Product2();
        prdct.Name='ijhabchg';
        prdct.IsActive=true;
        insert prdct;
      
        //Pricebook2 pbid = new Pricebook2();
        
        Id pricebookId = Test.getStandardPricebookId();
        
        
        
        Pricebook2 pb = new Pricebook2();
        pb.Name='Custom Pricebook';
        pb.IsActive=true;
        insert pb;
        
        PricebookEntry pbEntry1 = new PricebookEntry();
        pbEntry1.Pricebook2Id=pricebookId;
        pbEntry1.Product2Id=prdct.Id;
        pbEntry1.UnitPrice=2000;
        pbEntry1.IsActive=true;
        
        insert pbEntry1;
         
        
        PricebookEntry pbEntry = new PricebookEntry();
        pbEntry.Pricebook2Id=pb.id;
        pbEntry.Product2Id=prdct.Id;
        pbEntry.UnitPrice=2000;
        pbEntry.IsActive=true;
        
        insert pbEntry;
        
        OpportunityLineItem opLineItem = new OpportunityLineItem();
        opLineItem.OpportunityId=op.id;
        opLineItem.PricebookEntryId=pbEntry.id;
        opLineItem.UnitPrice=3;
        opLineItem.Quantity=21;
        
        insert opLineItem;
        
        YA3quest.script();
        
    }

}


If You find this answer is helpful for you then please mark best answer

Thank You!

Akshay Dhiman 63Akshay Dhiman 63
Hi Mikasa,

Below test class works fine please this out:-

@isTest
private class ScriptTest {

    @isTest static void scriptt(){
               Account acc = new Account(Name = 'Test');
        insert acc;
        
        Opportunity opp = new Opportunity(Name = 'Test Opp',AccountID = acc.Id,Amount = 100,CloseDate=Date.today(),StageName='Closed Won');
        insert opp;
        
        Id pricebookId = Test.getStandardPricebookId();
        
        Product2 prod = new Product2(
            Name = 'abc',
            ProductCode = '123',
            isActive = true
        );
        insert prod;
        
        PricebookEntry pbEntry = new PricebookEntry(
            Pricebook2Id = pricebookId,
            Product2Id = prod.Id,
            UnitPrice = 10.00,
            IsActive = true
        );
        insert pbEntry;
        
        OpportunityLineItem oli = new OpportunityLineItem(
            OpportunityId = opp.Id,
            Quantity = 5,
            PricebookEntryId = pbEntry.Id,
            TotalPrice = 5 * pbEntry.UnitPrice
        );
        insert oli;
                
        Test.startTest();
            Class_Name.script(); 
        Test.stopTest();
    }
        }
    }
    
Hope this explanation will resolve your query. Mark it as the best answer if you find it helpful.
Thanks
Akshay
This was selected as the best answer