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
Mont MontMont Mont 

Test for controller with OpportunityLineItem insert

I really don't understand, how to test that. Become an error.
Help me, please.

My code:

public void click(){
        
        for(WrapperClass wrp : wrapperRecordList){
            
  			if(wrp.isSelected){
                OpportunityLineItem opp = new OpportunityLineItem();
                opp.Product2Id = wrp.productRecord.Id;
                opp.Quantity = wrp.quantity;
                opp.OpportunityId = prodId;
                
                if(opp.PricebookEntryId != null){
                    opp.PricebookEntryId = [SELECT Id FROM PricebookEntry WHERE Pricebook2Id IN (SELECT Pricebook2Id FROM Opportunity WHERE Id =: prodId) AND Product2Id =: wrp.productRecord.Id LIMIT 1].Id;
                	opp.TotalPrice = wrp.quantity * [SELECT UnitPrice FROM PricebookEntry WHERE Pricebook2Id IN (SELECT Pricebook2Id FROM Opportunity WHERE Id =: prodId) AND Product2Id =: wrp.productRecord.Id LIMIT 1].UnitPrice;
                }
                
                try {
                    insert opp; 
                } catch(Exception e) {
                    ApexPages.addMessage(new ApexPages.Message(ApexPages.SEVERITY.ERROR, e.getMessage()));
                }    
            } 
 		}
 	}

My incorrect test:

@isTest
public class controllerTest {
    
    static testMethod void testMyController(){
        test.startTest();
        
        Pricebook2 pricebook = new Pricebook2(Name = 'new prbook');
        Opportunity opport = new Opportunity(Name = 'new opp', CloseDate = date.today(), StageName = 'Prospecting', Pricebook2Id = pricebook.Id);
        Product2 product = new Product2(Name = 'prod');
        
        OpportunityLineItem oppLine = new OpportunityLineItem();
        oppLine.Product2Id = product.Id;
        oppLine.Quantity = 2;
        oppLine.OpportunityId = opport.Id;
        
        insert oppLine;
        
        ProductController ProductControllerObj = new ProductController();
        ProductControllerObj.click();
        test.stopTest();        
    }
}