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
Sergio N 1Sergio N 1 

Testing for size

Hi All,

I have the following code and test already working, but was wondering how to cover the bolded piece in testing.

CODE
public class OPSel {
    
    public List<OpportunityLineItemWrapperCls> lineItemList {get;set;}
    public Set<String> selOLIs {get;set;}
    public Boolean hasSelOLI {get;set;}
    public ID opportunityID {get;set;}
    public boolean Button_Disabled{get; set;}
    
    public OPSel(ApexPages.StandardController controller){
        
        try{
            opportunityID=controller.getRecord().Id;
            lineItemList = new List<OpportunityLineItemWrapperCls>();
            selOLIs = new Set<String>();
            
            for(OpportunityLineItem oli : [SELECT ID,
                                           Opportunityid,
                                           Pricebookentry.name,
                                           listprice,
                                           Quantity,
                                           UnitPrice,
                                           Description
                                           FROM OpportunityLineItem WHERE OpportunityId = :controller.getRecord().Id order by createddate]){
                                               lineItemList.add(new OpportunityLineItemWrapperCls(oli));
                                           }
            //TESTING              
            system.assertEquals(controller.getRecord().Id, '1');
            //END TESTING
            
            if(lineItemList.Size()<2)
            {
                ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.WARNING,'You only split an opportunity with two or more line items');
                ApexPages.addMessage(myMsg);
                Button_Disabled=true;

            }
        }
        catch (Exception e)
        {
            ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.FATAL,e.getMessage());
            ApexPages.addMessage(myMsg);
        }
    }

TEST
@isTest(SeeAllData=True)
private class test{
    static testMethod void testclass() {
    Account acc = new Account(Name='Count');
    insert acc;
    
    Program__c testprog = new Program__c();
    testprog.name = Date.today().year() + ' Program';
    testprog.Start_Date__c = Date.today();
    testprog.End_Date__c = Date.today();
    insert testprog;
        
    Product2 pr = new Product2();
    pr.name='TestPr';
    pr.Program__c=testprog.id;
    insert pr;
    
    Pricebook2 PB = new Pricebook2();
       PB.name='Testbook';
       PB.IsActive=true;
    insert PB;
     Pricebook2 standardPricebook = [SELECT Id FROM Pricebook2 WHERE IsStandard=true];
        PricebookEntry X = new PricebookEntry();
        X.Product2ID=pr.id;
        X.Pricebook2Id=standardPricebook.Id;
        X.UnitPrice=1;
        X.IsActive=true;
        insert X;
        
    Opportunity TestOpp = new Opportunity();
    TestOpp.name='testOppty';
    TestOpp.AccountId=acc.id;
    TestOpp.StageName='Maximizer';
    TestOpp.CloseDate=System.today();
    insert TestOpp;
    
    Opportunity TestOpp2 = new Opportunity();
    TestOpp2.name='testOppty2';
    TestOpp2.AccountId=acc.id;
    TestOpp2.StageName='Maximizer';
    TestOpp2.CloseDate=System.today();
    insert TestOpp2;
        
    OpportunityLineItem TestOLI = new OpportunityLineItem();
    TestOLI.OpportunityId = TestOpp.id;
    TestOli.PricebookEntryId = X.Id;
    insert TestOLI;
     
    OpportunityLineItem TestOLI2 = new OpportunityLineItem();
    TestOLI2.OpportunityId = TestOpp.id;
    TestOli2.PricebookEntryId = X.Id;
    insert TestOLI2;
        
    test.startTest();
        PageReference pageRef = Page.OpportunityLineItemSelector;
        Test.setCurrentPage(pageRef);
        pageRef.getParameters().put('id',TestOpp.id);
        OpportunityLineItemSelectorCntrlr controller = new OpportunityLineItemSelectorCntrlr(new ApexPages.StandardController(TestOpp));
        OpportunityLineItemSelectorCntrlr controller2 = new OpportunityLineItemSelectorCntrlr(new ApexPages.StandardController(TestOpp2));
        controller.split();
        controller2.split();
        update TestOpp;
        update TestOpp2;
        OpportunityLineItemSelectorCntrlr c1 = new OpportunityLineItemSelectorCntrlr(new ApexPages.StandardController(TestOpp));
        
        Debugger debug=new Debugger();
        debug.a();
        debug.b();
        debug.c();
        debug.d();
    test.stopTest();
    }
}
 
3 Creeks3 Creeks
You can test for ApexPages Messages by either retrieving the messages for the current page using ApexPages.getMessages() and then looping through the messages to determine if they are the messages that you expect or by using ApexPages.hasMessages() to determine if there are any message on the page.
Amit Chaudhary 8Amit Chaudhary 8
Try to update your code (add one more method) like below
@isTest(SeeAllData=True)
private class test{
    static testMethod void testclass() 
	{
		Account acc = new Account(Name='Count');
		insert acc;
    
		Program__c testprog = new Program__c();
		testprog.name = Date.today().year() + ' Program';
		testprog.Start_Date__c = Date.today();
		testprog.End_Date__c = Date.today();
		insert testprog;
        
		Product2 pr = new Product2();
		pr.name='TestPr';
		pr.Program__c=testprog.id;
		insert pr;
    
		Pricebook2 PB = new Pricebook2();
		   PB.name='Testbook';
		   PB.IsActive=true;
		insert PB;
		
		Pricebook2 standardPricebook = [SELECT Id FROM Pricebook2 WHERE IsStandard=true];
		 
		PricebookEntry X = new PricebookEntry();
			X.Product2ID=pr.id;
			X.Pricebook2Id=standardPricebook.Id;
			X.UnitPrice=1;
			X.IsActive=true;
		insert X;
        
		Opportunity TestOpp = new Opportunity();
			TestOpp.name='testOppty';
			TestOpp.AccountId=acc.id;
			TestOpp.StageName='Maximizer';
			TestOpp.CloseDate=System.today();
		insert TestOpp;
    
		Opportunity TestOpp2 = new Opportunity();
			TestOpp2.name='testOppty2';
			TestOpp2.AccountId=acc.id;
			TestOpp2.StageName='Maximizer';
			TestOpp2.CloseDate=System.today();
		insert TestOpp2;
        
		OpportunityLineItem TestOLI = new OpportunityLineItem();
		TestOLI.OpportunityId = TestOpp.id;
		TestOli.PricebookEntryId = X.Id;
		insert TestOLI;
     
		OpportunityLineItem TestOLI2 = new OpportunityLineItem();
		TestOLI2.OpportunityId = TestOpp.id;
		TestOli2.PricebookEntryId = X.Id;
		insert TestOLI2;
        
    test.startTest();
	
        PageReference pageRef = Page.OpportunityLineItemSelector;
        Test.setCurrentPage(pageRef);
        pageRef.getParameters().put('id',TestOpp.id);
		
        OpportunityLineItemSelectorCntrlr controller = new OpportunityLineItemSelectorCntrlr(new ApexPages.StandardController(TestOpp));
        OpportunityLineItemSelectorCntrlr controller2 = new OpportunityLineItemSelectorCntrlr(new ApexPages.StandardController(TestOpp2));
		
        controller.split();
        controller2.split();
        update TestOpp;
        update TestOpp2;

        OpportunityLineItemSelectorCntrlr c1 = new OpportunityLineItemSelectorCntrlr(new ApexPages.StandardController(TestOpp));
        
		
        Debugger debug=new Debugger();
        debug.a();
        debug.b();
        debug.c();
        debug.d();
    test.stopTest();
    }
	
    static testMethod void testclass1() 
	{
		Account acc = new Account(Name='Count');
		insert acc;
    
		Program__c testprog = new Program__c();
		testprog.name = Date.today().year() + ' Program';
		testprog.Start_Date__c = Date.today();
		testprog.End_Date__c = Date.today();
		insert testprog;
        
		Product2 pr = new Product2();
		pr.name='TestPr';
		pr.Program__c=testprog.id;
		insert pr;
    
		Pricebook2 PB = new Pricebook2();
		   PB.name='Testbook';
		   PB.IsActive=true;
		insert PB;
		
		Pricebook2 standardPricebook = [SELECT Id FROM Pricebook2 WHERE IsStandard=true];
		 
		PricebookEntry X = new PricebookEntry();
			X.Product2ID=pr.id;
			X.Pricebook2Id=standardPricebook.Id;
			X.UnitPrice=1;
			X.IsActive=true;
		insert X;
        
		Opportunity TestOpp = new Opportunity();
			TestOpp.name='testOppty';
			TestOpp.AccountId=acc.id;
			TestOpp.StageName='Maximizer';
			TestOpp.CloseDate=System.today();
		insert TestOpp;
    
		Opportunity TestOpp2 = new Opportunity();
			TestOpp2.name='testOppty2';
			TestOpp2.AccountId=acc.id;
			TestOpp2.StageName='Maximizer';
			TestOpp2.CloseDate=System.today();
		insert TestOpp2;
        
		OpportunityLineItem TestOLI = new OpportunityLineItem();
		TestOLI.OpportunityId = TestOpp.id;
		TestOli.PricebookEntryId = X.Id;
		insert TestOLI;
     
        
    test.startTest();
	
        OPSel controller = new OPSel(new ApexPages.StandardController(TestOpp));
		
   
    test.stopTest();
    }
	
}
let us know if this will help you